2012-03-27 70 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