开发者

Java (Android) Html Table Parsing to Database (SQLite)

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 =    开发者_如何学C "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);
    }
    }
}


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);




       }
}


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 :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜