JSoup获取表的内容

时间:2014-10-04 04:48:49

标签: java android android-studio jsoup

我尝试使用JSoup for Android提取温度值(在下表51.46中)和6.43表压力来自下表。请注意,随着新的温度和压力读数的变化,51.46和6.43会发生变化

<tr class="time"><td colspan="3">at 10-03-2014 23:15:00</td></tr>
<tr><td class="param" title="Temperature" rel="tooltip">Temperature</td><td class="value">51.46</td><td>F</td></tr>
<tr><td class="param" title="Pressure" rel="tooltip">Pressure</td><td class="value">6.43</td><td>psi</td></tr>
<tr><td class="param" title="Level" rel="tooltip">Level</td><td class="value">-1.00</td><td>ft</td></tr>
<tr><td class="param" title="Cell End" rel="tooltip">Cell End</td><td class="value">13.100</td><td>ft</td></tr>

1 个答案:

答案 0 :(得分:0)

这应该有效。请记住,您需要在中间使用s,或者在将其解析为Jsoup文档时将其删除。

File file = new File("test2.html");
try{
    Document doc = Jsoup.parse(file, "UTF-8");
    Element temperatureTd = doc.select("td[title=Temperature]").first();
    Element pressureTd = doc.select("td[title=Pressure]").first();

    String temperature = temperatureTd.nextElementSibling().text() + " "
            + temperatureTd.nextElementSibling().nextElementSibling().text();
    System.out.println(String.format("Temperature: %s", temperature));
    String pressure = pressureTd.nextElementSibling().text() + " "
            + pressureTd.nextElementSibling().nextElementSibling().text();
    System.out.println(String.format("Pressure: %s", pressure));

} catch (Exception e){
    e.printStackTrace();
}