Skip to content Skip to sidebar Skip to footer

How To Parse The Cells Of The 3rd Column Of A Table?

I am trying to parse the cells of the 3rd column of a using Jsoup. Here is the HTML:

Solution 1:

This approach is quite a mess and you didn't tell anything about at which line the NPE occurred, so it's hard to give a straight answer to your question.

Apart from that, I would suggest to not do it the hard and error prone way. As that <table> has already an id attribute which is supposed to be unique throughout the document, just use the ID selector #someid. Further, you can get the cells of the 3rd column using the index selector :eq(index) (note: it's zero based!).

So, those few of simple lines should do it:

Document document = Jsoup.connect("http://wap.nastabuss.se/its4wap/QueryForm.aspx?hpl=Teleborg+C+(V%C3%A4xj%C3%B6)").get();
Elements nextTurns = document.select("#GridViewForecasts td:eq(2)");

for (Element nextTurn : nextTurns) {
    System.out.println(nextTurn.text());
}

which results here in:

50
30
10
18
3
24

That's it.

I strongly recommend to invest some time in properly learning the CSS selector syntax as Jsoup is build around it.

See also:


Solution 2:

I think the best solution is to use get(); method to get single element from number of elements.

Document doc = Jsoup.connect("your_url").get();
Elements table = doc .getElementById("id_of_your_table");
Element tr = table.select("tr").get(2); // this will get 3rd tr
//if you need 3rd column of 3rd row then 
Element 3rdtd = tr.select("td").get(2);
Log.i("3rd Column of 3rd Row", 3rdtd.text().toString());

Hope it will help.


Post a Comment for "How To Parse The Cells Of The 3rd Column Of A Table?"