Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select text in HTML tag without a tag around it (JSoup)

Tags:

java

html

dom

jsoup

I would like to select the text inside the strong-tag but without the div under it...

Is there a possibility to do this with jsoup directly?

My try for the selection (doesn't work, selects the full content inside the strong-tag):

Elements selection = htmlDocument.select("strong").select("*:not(.dontwantthatclass)");

HTML:

<strong>
   I want that text
   <div class="dontwantthatclass">
   </div>
</strong>
like image 330
Ferb Avatar asked Jun 23 '16 12:06

Ferb


1 Answers

You are looking for the ownText() method.

String txt = htmlDocument.select("strong").first().ownText();
like image 53
junijo Avatar answered Oct 31 '22 05:10

junijo