0

I have searched far and wide for a number of days now trying to find the answer to the following problem. I am pretty new to Java and Android programming and have learnt mainly from copy-paste-edit scenarios, but despite there being a few tutorials around I am really struggling with this one.

I have tried htmlcleaner as my parser, and I'm sure I'm close but just cant get this right.

I am trying to parse a table of values on a website (i.e. <TR>TD,TD,TD,TD</TR>) into a standard Android SQLite database. So each row of the table would be a row in the db.

In theory this should be quite simple but I am getting snowed under with different methods and not understanding a simple clean way to do it.

Can anybody help? Please don't point me to a htmlparser or non-specific tutorial because I have worked at these for ages and can't manage a solution.

The table values I am trying to parse are here: Teams and values

EDIT:::

OK I think I'm getting close using JSoup. I can grab a value from within a [td] tag and put it to a string. However, how can I adapt the following code so that each row (i.e. within each [tr]) is iterated for each 'for', and each [td] value is put to a String array?

public class GetTable extends Activity {

static String hello;


@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.testlayout);
try {
    populateTexts();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

    private void populateTexts() throws IOException{
    String url =     "http://news.bbc.co.uk/mobile/bbc_sport/rugby/competition/ru100/table/index.shtml?context=cps_ukfs";

    Document doc = Jsoup.connect(url).get();
  Elements links = doc.select("td");


    for (Element link : links) {
        hello = link.text();
        TextView twittertext10 = (TextView)findViewById(R.id.testView1);
        twittertext10.setText(hello);
    }
    }
}
3
  • Where is your problem more specificaly ? Show some code to indicate where your problem is. I personally can't figure it out. Commented Jun 12, 2011 at 22:37
  • Sorry, it is difficult to provide code without convoluting the question, as my problem is that I cannot find the right code. I am trying, in simple terms, to take the data from the table in the link (Team names, games played, points etc) and populate an Android SQLite database. So each row in the table would be the same as each row in the SQLite table. Commented Jun 13, 2011 at 14:39
  • I have done this successfully parsing from an iCal file, but I am really struggling to iterate through a html table and populate the db. I have tried htmlcleaner but I cant find a good tutorial which shows how to populate a database or recursively populate strings with the results. They all seem to populate lists. Thanks in advance Commented Jun 13, 2011 at 14:44

2 Answers 2

2

Solution is as follows. I output the database to a listview just to show I had all the correct data.

public class GetTable extends ListActivity {

static String team;
static int played;
static int won;
static int drew;
static int points;
private TableDbAdapter mDbHelper;
static int position = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.testlayout);
    mDbHelper = new TableDbAdapter(this);
       mDbHelper.open();
    mDbHelper.clearTable();
try {
    populateTexts();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
populateList();
}

    private void populateTexts() throws IOException{
    String url =     "http://news.bbc.co.uk/mobile/bbc_sport/rugby/competition/ru100/table/index.shtml?    context=cps_ukfs";

    Document doc = Jsoup.connect(url).get();

    //

  Elements rows = doc.select("table[class=tblResults teamTable] tr:gt(0):lt(13)");

        String s[] = new String[rows.size()];
        for(Element row : rows){
            s[0] = row.child(0).text();
            s[1] = row.child(1).text();
            s[2] = row.child(2).text();
            s[3] = row.child(3).text();
            s[4] = row.child(4).text();

        team = s[0];
        if (s[1] !=""){
        played = Integer.parseInt(s[1]);}
        else played = 0;
        if (s[2] !=""){
        won = Integer.parseInt(s[2]);}
        else won = 0;
        if (s[3] !=""){
        drew = Integer.parseInt(s[3]);}
        else drew = 0;
        if (s[4] !=""){
        points = Integer.parseInt(s[4]);}
        else points = 0;

        position ++;


    // sql insert
       mDbHelper.createTableRow(position, team, played, won, drew, points);



    }
    }

    private void populateList() {
           // Get all of the fixtures from the database and create the item list
           Cursor c = mDbHelper.fetchWholeTable();
           startManagingCursor(c);

           String[] from = new String[] { TableDbAdapter.KEY_POSITION, TableDbAdapter.KEY_TEAM, TableDbAdapter.KEY_PLAYED, TableDbAdapter.KEY_WINS, TableDbAdapter.KEY_DREW, TableDbAdapter.KEY_POINTS};
           int[] to = new int[] { R.id.fixtextlist, R.id.fixtextlistkotime, R.id.fixtextlisthome, R.id.fixtextlisths, R.id.fixtextlistas, R.id.fixtextlistaway};

           // Now create an array adapter and set it to display using our row
           SimpleCursorAdapter table =
               new SimpleCursorAdapter(this, R.layout.fixlist_item, c, from, to);
           setListAdapter(table);




       }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You probably would have best luck using SAX or some other XML parser to parse out your table data, then from there you use the SQLIteDatabaseHelper class to make a table and insert your data. If you have a more specific question I will try to give a more specific answer :-)

1 Comment

Thanks, sorry for the kinda loose question. I have solved it now and edited with my solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.