2014-04-09 111 views
0

我已經下載jsoup並複製到/ libs,但我有一個大問題。我不知道解析這張表並從標籤中獲得價值。 我有這個表...JSOUP解析表

<table class="tabella-voli"> 
    <thead> 
     <th>Compagnia</th> 
     <th>N.</th> 
     <th>Provenienza</th> 
     <th>Schedulato</th> 
     <th>Stimato</th> 
     <th>Stato</th> 
    </thead> 
    <tbody> 
        <tr style="background-color: rgba(253, 253, 253, 0.8);"> 
      <td>RYANAIR</td> 
      <td>05021</td> 
      <td>Roma Ciampino</td> 
      <td>09/04/2014 13:10</td> 
      <td>09/04/2014 12:58</td> 
      <td> 
            <img src="/images/volo_green.gif" alt="Atterrato" title="Atterrato"/><br /> Atterrato    </td> 
     </tr> 
        <tr style="background-color: rgba(253, 253, 253, 0.8);"> 
      <td>RYANAIR</td> 
      <td>01411</td> 
      <td>Pisa</td> 
      <td>09/04/2014 17:50</td> 
      <td>09/04/2014 18:00</td> 
      <td> 
            <img src="/images/volo_green.gif" alt="In orario" title="In orario"/><br /> In orario    </td> 
     </tr> 
       </tbody> 
</table> 

我想解析只有這個:

<td>RYANAIR</td> 
      <td>05021</td> 
      <td>Roma Ciampino</td> 
      <td>09/04/2014 13:10</td> 
      <td>09/04/2014 12:58</td> 
      <td> 

和...

   <td>RYANAIR</td> 
      <td>01411</td> 
      <td>Pisa</td> 
      <td>09/04/2014 17:50</td> 
      <td>09/04/2014 18:00</td> 
      <td> 

,並打印成一個TextView。 這是我的代碼:

   org.jsoup.nodes.Document doc = Jsoup.connect("http://mysite...").get(); 

      Element tabella = doc.getElementsByClass("tabella-voli").first(); 
      Elements tbody = doc.getElementsByTag("tbody"); 
      Elements element = tbody; 
      System.out.println(element.text()); 

你能幫我嗎?對不起我的英語不好! 我是新手:) :) 謝謝你們!

+0

對於每個'table.tabella-辦妥機票tr',檢查第一'td'的文本內容爲* RYANAIR *。如果是肯定的,請提取'tr'的其餘部分。 – MCL

回答

0

處理此問題的最佳方法是首先選擇tr元素,然後繼續並選擇該td下的每個子單元格(td)。這看起來像:

Elements tableRows = doc.select("table.tabella-voli > tbody > tr"); 

然後去頭,並從該行獲得的每個細胞,印刷是這樣的:

for (Element tableRow : tableRows) { 
    System.out.println("New Row:"); 
    Elements tableRowCells = tableRow.select("> td"); 
    for (Element tableRowCell : tableRowCells) { 
      System.out.println(tableRowCell.ownText()); 
    } 
} 

希望這有助於!

0

試試下面的代碼:

Element table = doc.select("table[class=tabella-voli]").first(); 

Iterator<Element> ite = table.select("td").iterator(); 

System.out.println("Value 1: " + ite.next().text()); 
System.out.println("Value 2: " + ite.next().text()); 
System.out.println("Value 3: " + ite.next().text()); 
System.out.println("Value 4: " + ite.next().text());