2014-10-04 99 views
0

的內容我試圖從下表中使用JSoup for Android提取溫度值(在下表中是51.46)和6.43中的壓力。請注意,51.46和6.43隨着新的溫度和壓力讀數而變化JSoup獲取表

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

回答

0

這應該有效。請記住,您需要介於兩者之間,否則當您將其解析爲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(); 
}