cchinchoy cchinchoy - 18 days ago 9
Android Question

Android JSOUP Parsing Tables fromWeb Sites

I am having an issue with parsing the following website:

http://www.x-rates.com/d/USD/table.html

I am very, very very new to android programming and I am thinking about creating a currency converter, now I know that there is a bunch out there however, I am trying to fast track my programming initiation and get myself immersed in code.

I found a tutorial that tried to explain and it was helpful for a website with the need for a specific item. the code is below:

package com.ve.webgrab;

import java.io.IOException;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;

public class WebgrabActivity extends ListActivity {

public void onCreate(Bundle icicle) {
super.onCreate(icicle);

ArrayList<String> tableRowStrings = new ArrayList<String>();

Document doc = null;
try {
doc = Jsoup.connect("http://www.x-rates.com/d/USD/table.html").get();
}
catch (IOException e) {
e.printStackTrace();
}

String testString = "American";
Elements tableRows = doc.select("tr");
for (Element tableRow : tableRows){
if (tableRow.hasText()){
String rowData = tableRow.text();
if(rowData.contains(testString)){
tableRowStrings.add(rowData);
}
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tableRowStrings);
setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}


I need to be able to get the table with the usd to other currency so that I can have it in a database and then use it for the conversion. I believe the section that I need to look at is:

String testString = "American";
Elements tableRows = doc.select("tr");
for (Element tableRow : tableRows){
if (tableRow.hasText()){
String rowData = tableRow.text();
if(rowData.contains(testString)){
tableRowStrings.add(rowData);
}
}
}


What I need to accomplish:


  • Get the website:- - I believe this is accomplished:-

    doc = Jsoup.connect("http://www.x-rates.com/d/USD/table.html").get();

  • Select the table on the website with particular interest on the Conversion rate: - Still cannot figure this part out.

  • Test for the Table to ensure it is the correct one:

    String testString = "American";

    if(rowData.contains(testString)){

  • Take the data on the table and display to a list view just to ensure that the correct data is being gathered:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tableRowStrings);
    setListAdapter(adapter);

  • Break up the table
    <td>
    into country and conversion rate and then place it into a database.



I am looking for guidance, please remember that I am totally new to this and I want to build out this app for a learning experience and a spring board to bigger and better apps, example code is definitely welcome, I would appreciate all the help I can get.

Answer

I've upvoted Marvin's answer because using a webservice that Yahoo provides is going to probably be more reliable in the long run.

However, I have whipped up a basic solution for you using another website. You can see how much less code is needed when the site uses classes and ids to help you select the right things versus iterating through all of the elements and looking for certain things to find the information you want. You'll need to fixup this code a little bit to check for errors and to suit your needs, but it should help you get started if you still want to go down this path.

    //Get the HTML from the source
    Document doc = Jsoup.connect("http://www.exchangerate.com/").get();
    //select all the tr elements within the tbody elements within
    //the table with class name table_text_small
    //and iterate over all of those elements
    for (Element e : doc.select("table.table_text_small > tbody > tr")) {
        //this is a little hackish, but check to make sure this tr has
        //at least 5 children (tds)
        if (e.children().size() >= 5) {
            //if so, print out the 1st child (country name)
            //and the 5th child (exchange rate)
            System.out.println(e.child(1).text() + ":" + e.child(4).text());
        }
    }
Comments