简体   繁体   中英

JSoup To Get Contents of Table

I'm trying to extract the temperature value (in the table below 51.46) and the 6.43 for the pressure from the table below using JSoup for Android. Please note that that 51.46 and 6.43 change as new temperature and pressure readings are taken

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

This should work. Keep in mind that you need to have the s in between or it will be stripped off when you are parsing it as a Jsoup Document.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM