2015-11-08 25 views
0

我需要幫助從JSOUP獲取HTML中的字符串。使用JSOUP從HTML中獲取字符串

該文件是建立像:

<body> 
    <span class="a-touch"> 
     <div class"a-container"> 
     <div class"a-box"> 
      <div class="a-row a-spacing-small"> 
       <b>string1</b><br/>string2 97<br/>String3 
       <br/>string4</>string5<br/> 
      </div> 

現在我需要得到的字符串。 我使用Google搜索,但只能找到表格的例子等等。

回答

0

下面的代碼可以讓你的strings數組,它包含了a-row div的文本內容,由換行符分割:

Document doc = Jsoup.parseBodyFragment(html); 
Elements a_row_div = doc.select(".a-row"); 
String[] strings = Jsoup.clean(a_row_div.html(), "", Whitelist.none(), 
    new OutputSettings().prettyPrint(false)).split("\n"); 
0

的字符串都存儲在TextNode S IN JSoup。使用(Node n : Element.childNodes()集合來遍歷所有節點。通常相關的唯一節點是Element或TextNode類型。使用if (n instanceof TextNode)測試並在所有innerText上運行,並使用if (n instanceof Element)對所有子元素進行遞歸調用。

+0

我在這裏遇到的問題是,在這裏還有一些其他的「div分支」,其中也有一些文字。 我上面顯示的代碼只是我需要的分支。 但很高興知道,我現在可以用它來解決另一個問題! – Hunter