-1

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
cchinchoy
  • 51
  • 2
  • 12
  • That website makes it tricky to extract what you want because it doesn't use any ids or class names to indicate what or where anything is located. Are you committed to this particular website or could you use another one that might have some handles for better selects? – B. Anderson Feb 01 '12 at 18:44
  • I am not stuck on the website, I am open to choices and as I said before I am very new. – cchinchoy Feb 02 '12 at 14:10

2 Answers2

1

If all you're looking to do is currency conversions, I would recommend you use the (free) Yahoo API to get this done. You can read all about the details in this answer, but essentially you'll pull something like the following:

http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=sl1d1t1ba&e=.csv

Which will convert from GBP to EUR and produce an easily parseable text file. As @B.Anderson already pointed out, it's going to be quite tricky using JSoup on that website.

Community
  • 1
  • 1
Marvin Pinto
  • 30,138
  • 7
  • 37
  • 54
1

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());
        }
    }
B. Anderson
  • 3,079
  • 25
  • 33
  • Hey, this looks great, As I have said previously, I am very new to this and I am willing to try anything and thus growing my knowledge. – cchinchoy Feb 03 '12 at 19:29
  • I believe that I have understood your code and integrated slightly taking into consideration the aspects of the ISO tag which is not really a country code but a tag as to what the country codes are. See Below for the modification: for (Element e : doc.select("table.table_text_small > tbody > tr")) { if (e.children().size() >= 5) { if(e.child(3).text().equals(ignore)){ String rowData = ""; }else { String rowData = e.child(3).text() + ":" + e.child(5).text(); tableRowStrings.add(rowData); – cchinchoy Feb 04 '12 at 15:39