Getting specific text after tag using jsoup

Hi, I am trying to get specific text from an HTML file using jsoup. I already know how to get text2 and text3. But how do I get the text I want without others?
<div class="snt"> text I want  
<br clear="both" />text2  
<br clear="both" />text3  
<br clear="both" />  
</div>    

I tried to use

Elements lines = doc.select(".snt");
lines.First().nextSibling().toString();    

but I get nothing. I also tried:

Elements lines = doc.select(".snt");  
lines.text(); // this return all texts together       

Could you help me? Thank you for your responses.

+4
source share
1 answer

If you try ownText () for the first element, you will get "the text I want text2 text3" and that is correct. You want the text before br, and this is the first child node under your first element. Jsoup treats the text as a node.

Elements lines = doc.select(".snt");
System.out.println(lines.first().childNodes().get(0));
+1
source

All Articles