3

I have the following HTML code:

<div class=example>Text #1</div> "Another Text 1"
<div class=example>Text #2</div> "Another Text 2"

I want to extract the Text outside the tag, "Another Text 1" and "Another Text 2"

I'm using JSoup to achieve this.

Any ideas???

Thanks!

4

2 に答える 2

2

各タグの次のNode( Element! ではない) を選択できますdiv。あなたの例では、それらはすべてTextNodeです。

final String html = "<div class=example>Text #1</div> \"Another Text 1\"\n"
                  + "<div class=example>Text #2</div> \"Another Text 2\" ";

Document doc = Jsoup.parse(html);

for( Element element : doc.select("div.example") ) // Select all the div tags
{
    TextNode next = (TextNode) element.nextSibling(); // Get the next node of each div as a TextNode

    System.out.println(next.text()); // Print the text of the TextNode
}

出力:

 "Another Text 1" 
 "Another Text 2" 
于 2013-11-10T18:51:12.103 に答える