Jsoup text "subtraction" in a div containing

Using jsoup, I know how to extract the text of an entire div:

<div class="c">
<a href="/relurl.php?refid=7">First Anchor Text</a>
 Something in Between 
 <a href="/john.doe?refid=7">Second Anchor Text</a>
</div>

Thus, div.text()it gives:

First anchor text Something in between Second anchor text

And I know how to extract the text of each anchor separately, so the first one a.text()gives:

First anchor text

But is there an elegant way in Jsoup to extract only Something in Between?

(I can, of course, extract 2 a.text()and “subtract” them from div.text(), but I do not find this elegant)

+3
source share
1 answer

Use Element#ownText(). Here is an excerpt from the related javadoc:

ownText

public String ownText()

Gets text that belongs only to this element; Does not receive the combined text of all children.

, HTML <p>Hello <b>there</b> now!</p>, p.ownText() "Hello now!", p.text() "Hello there now!". , b , p.

, :

String ownText = div.ownText();
// ...
+5

All Articles