Ke7in
Ke7in

Reputation: 927

Getting data inside <div><p>data</p></div>

I am trying to get the data out from the following tag.

I have done this.

Document doc = Jsoup.parse(currMsg);
Elements ele = doc.select("p");

This returns <p>data</p> I want data only.

then i tried to traverse an get char by char.

I want to know is there any other way i can get the data easily.

Upvotes: 0

Views: 86

Answers (1)

Jeffrey Bosboom
Jeffrey Bosboom

Reputation: 13653

Get the text of an element with Element.text() or Element.ownText(). text() returns all text inside the element, including inside child elements, while ownText() returns text only in that element (not in any child elements). Element.textNodes() gives you finer-grained control, if you want some but not all of the text.

The textNodes() Javadoc gives a tiny example showing the relationship between the different ways to get text:

For example, with the input HTML: <p>One <span>Two</span> Three <br> Four</p>
with the p element selected:

p.text() = "One Two Three Four"
p.ownText() = "One Three Four"
p.children() = Elements[<span>, <br>]
p.childNodes() = List<Node>["One ", <span>, " Three ", <br>, " Four"]
p.textNodes() = List<TextNode>["One ", " Three ", " Four"]

This is covered in the Jsoup Cookbook. You may find the other sections of the cookbook helpful.

Upvotes: 1

Related Questions