I have a table

enter image description here

with this html code

<table class="grid"> <colgroup span="1" /> <colgroup width="7.1%" span="12" /> <tr><th>Year</th><th colspan="12">Month</th></tr> <tr> <td>2011</td> <td colspan="8">&nbsp;</td> <td><a href="https://www.gokgs.com/gameArchives.jsp?user=asdkk&amp;oldAccounts=t&amp;year=2011&amp;month=9">Sep</a></td> <td><a href="https://www.gokgs.com/gameArchives.jsp?user=asdkk&amp;oldAccounts=t&amp;year=2011&amp;month=10">Oct</a></td> <td><a href="https://www.gokgs.com/gameArchives.jsp?user=asdkk&amp;oldAccounts=t&amp;year=2011&amp;month=11">Nov</a></td> <td><a href="https://www.gokgs.com/gameArchives.jsp?user=asdkk&amp;oldAccounts=t&amp;year=2011&amp;month=12">Dec</a></td> </tr> <tr> <td>2012</td> <td><a href="https://www.gokgs.com/gameArchives.jsp?user=asdkk&amp;oldAccounts=t&amp;year=2012&amp;month=1">Jan</a></td> <td><a href="https://www.gokgs.com/gameArchives.jsp?user=asdkk&amp;oldAccounts=t&amp;year=2012&amp;month=2">Feb</a></td> <td><a href="https://www.gokgs.com/gameArchives.jsp?user=asdkk&amp;oldAccounts=t&amp;year=2012&amp;month=3">Mar</a></td> <td><a href="https://www.gokgs.com/gameArchives.jsp?user=asdkk&amp;oldAccounts=t&amp;year=2012&amp;month=4">Apr</a></td> <td colspan="8">&nbsp;</td> </tr> </table> 

I'm trying to parse the table and here is my code.

xml

 <GridView android:id="@+id/gridview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numColumns="13"> </GridView> 

Jsoup lib

  try { Document verb = Jsoup.connect("https://www.gokgs.com/gameArchives.jsp?user=" + nickname + "&oldAccounts=y").get(); Element tableVerb = verb.select("p ~ table.grid").first(); Elements rowsVerb = tableVerb.select("tr"); titleList.clear(); for (int i = 0; i < rowsVerb.size(); i++) { Element row = rowsVerb.get(i); Elements cols = row.select("td"); for (Element e: cols){ if (e.hasAttr("colspan")) { int colspan = Integer.valueOf(cols.attr("colspan")); for (int y = 0; y < colspan; y++){ titleList.add(y, e.text().toString()); } } else { titleList.add(e.text().toString()); } } } } catch (IOException e) { e.printStackTrace(); } 

As a result, I get the following table

enter image description here

My table has 13 columns (year + 12 months). If some months are missing in the table, then the <td colspan = "число">&nbsp;</ td> tag is used in the html code.

At me all empty cells are added at first, and it is necessary to repeat the table from a site. Where is the mistake?

  • one
    Integer.valueOf(cols.attr("colspan")); -> Integer.valueOf(e.attr("colspan")); titleList.add(y, e.text().toString()); -> titleList.add(e.text().toString()); - Yura Ivanov
  • Thanks, helped! - klieve

0