I ve looked at the JSoup Selector Overview documentation but I’m still having problems. I want to get information from a webpage and this information is stored in a table. Once I retrieve this I want to split into the individual rows in order to use it later; but I can’t seem to do it.
I have JSoup running fine but it doesn’t work when I try to reference the particular table I want by Id such as:
try
{
Document doc = Jsoup.connect( myHtml ).get();
Elements pTag = doc.select( "table#ctl00_ContentPlaceHolder1_tblPasses" );
//Elements links = doc.select( "a[href]" );
String pTagString = pTag.html();
// Set the textview to the results of the Jsoup query
setData( pTagString );
// Reset msg
msg = null;
msg = handler.obtainMessage( UPDATE_UI );
handler.sendMessage( msg );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here is the segment of HTML code Im attempting to get to:
<table id="ctl00_ContentPlaceHolder1_tblPasses" class="standardTable" cellspacing="0" cellpadding="4" rules="cols" border="2" style="background-color:White;border-color:Gray;border-width:2px;border-style:Solid;border-collapse:collapse;">
<tr class="tablehead" style="border-width:0px;border-style:None;">
<td valign="middle" rowspan="2">Date</td><td id="ctl00_ContentPlaceHolder1_MagHeader" valign="middle">Brightness</td><td align="center" colspan="3">Start</td><td align="center" colspan="3">Highest point</td><td align="center" colspan="3">End</td>
</tr><tr class="tablehead">
<td id="ctl00_ContentPlaceHolder1_MagHeader2" align="center">(<a id="ctl00_ContentPlaceHolder1_linkMagnitude" title="show definition of this term" href="glossary.aspx?term=magnitude&lat=54.5156&lng=-6.06863&loc=Unspecified&alt=0&tz=GMT">mag</a>)</td><td align="center">Time</td><td><a id="ctl00_ContentPlaceHolder1_linkAltitude" title="show definition of this term" href="glossary.aspx?term=altitude&lat=54.5156&lng=-6.06863&loc=Unspecified&alt=0&tz=GMT">Alt.</a></td><td><a id="ctl00_ContentPlaceHolder1_linkAzimuth" title="show definition of this term" href="glossary.aspx?term=azimuth&lat=54.5156&lng=-6.06863&loc=Unspecified&alt=0&tz=GMT">Az.</a></td><td align="center">Time</td><td>Alt.</td><td>Az.</td><td align="center">Time</td><td>Alt.</td><td>Az.</td>
This is only part of the table, there’s a lot more but for now I want to get at this table and it’s first row. Any thoughts?
Update
I tried this:
Elements table = doc.select( "table#ctl00_ContentPlaceHolder1_tblPasses" );
Element first_Row = table.first();
//Elements links = doc.select( "a[href]" );
String first_Row_Strg = first_Row.html().toString();
// Set the textview to the results of the Jsoup query
setData( first_Row_Strg );
But it actually returns the whole table and not the first row. Should I try aiming at the actual identifier of the row in the table? If so how do you do it?
think differently, select directly child from parent.
Element first_row = doc.select("table#ctl00_ContentPlaceHolder1_tblPasses tr").first();