用表解析JSOUP

时间:2014-04-09 16:29:08

标签: android parsing jsoup

我已下载jsoup并复制到/ libs但我遇到了一个大问题。我不知道解析这个表并从标签中获取价值。 我有这张桌子......

<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());
你可以帮帮我吗?对不起我的英语不好! 我是新手:) :) 谢谢你们!!!“

2 个答案:

答案 0 :(得分: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());
     }
}

希望这有帮助!

答案 1 :(得分: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());