2015-04-22 59 views
-2

我有這樣的HTML代碼Jsoup recove文本,而不選擇

<div itemprop="doseSchedule"> 
text1 
</div> 
<h3><a id="SP3">TITLE</a></h3> 
<div>text2</div> 
<div itemprop="warning"> 
text3 
</div> 

,我試圖恢復文本2,但仍然不能。我如何做到這一點?

+1

你試過到目前爲止什麼碼? \t \t \t爲(元素元素:選擇) –

+0

@TomMac我已使用此代碼解決{ \t \t \t \t \t字符串德圖= element.text(); \t \t \t \t \t如果(testo.contains( 「04.3」)){ \t \t \t \t \t \t字符串的子串= getPrecauzioni(德圖); \t \t \t \t \t \t parafarmaco.setPrecauzioni(substring.trim()); \t \t \t \t \t} \t \t \t \t \t如果(testo.contains( 「06.0」)){ \t \t \t \t \t \t字符串的子串= getCodiceGMP(德圖); \t \t \t \t \t \t parafarmaco.setCodiceGMP(substring.trim()); \t \t \t \t \t} \t \t \t \t} – Zaknafein

回答

0

該代碼會做你想要什麼:

public class JSoupTest { 
    public static void main(String[] args) throws IOException { 
    String html = "<div itemprop=\"doseSchedule\">\n" 
      + "text1\n" 
      + "</div>\n" 
      + "<h3><a id=\"SP3\">TITLE</a></h3>\n" 
      + "<div>text2</div>\n" 
      + "<div itemprop=\"warning\">\n" 
      + "text3\n" 
      + "</div>"; 

    Document doc = Jsoup.parse(html); 

    Element e = doc.select("h3").first().nextElementSibling(); 

    System.out.println(e.text()); 
    } 
} 
+0

非常感謝:d(它永遠不會太晚) – Zaknafein