2012-03-27 67 views
1

我看了一下JSoup Selector Overview文档,但我仍然有问题。我想从网页获取信息,并将此信息存储在表格中。一旦我检索到这个,我想分割成单独的行以便稍后使用;但我似乎无法做到。Jsoup和Android

我有JSoup运行良好,但是当我试图通过ID,例如引用特定的表我想这是行不通的:

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

这里是HTML代码林试图让该段:

<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&amp;lat=54.5156&amp;lng=-6.06863&amp;loc=Unspecified&amp;alt=0&amp;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&amp;lat=54.5156&amp;lng=-6.06863&amp;loc=Unspecified&amp;alt=0&amp;tz=GMT">Alt.</a></td><td><a id="ctl00_ContentPlaceHolder1_linkAzimuth" title="show definition of this term" href="glossary.aspx?term=azimuth&amp;lat=54.5156&amp;lng=-6.06863&amp;loc=Unspecified&amp;alt=0&amp;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> 

这只是表的一部分,还有很多,但现在我想要在这张表上,它是第一行。有什么想法吗?

更新

我尝试这样做:

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

但它实际上返回整个表,而不是第一行。我应该尝试瞄准表中该行的实际标识符吗?如果是的话,你如何做到这一点?

回答

1

不同地思考,直接从父母选择孩子。

Element first_row = doc.select("table#ctl00_ContentPlaceHolder1_tblPasses tr").first();

+0

嗨队友 - 只是为了更新你 - 是的,我得到它的工作,虽然我确切的方法是诱饵周围,我决定参考上一顶每个组件。像表>行>第一行> firstCell使用[HREF]等等等感谢您的帮助:D – Katana24 2012-03-27 19:11:39