Trevor
Trevor

Reputation: 2457

Select element but deselect specified child of element

Just curious if this is possible in jsoup

Select an element and it's text but deselect a specified child.

<div>
  <p><strong>Location: </strong> Earth</p>
</div>

Is it possible to return just Earth?

Edit: Also, in the paragraph, Location is static, but Earth is not. i.e you can have

Location: Earth
Location: LA
Location: Mars

Upvotes: 2

Views: 242

Answers (1)

Stephan
Stephan

Reputation: 43033

You want to use the Element::ownText method.

String html = "<div>\n" + //
        "<p><strong>Location: </strong> Earth</p>\n" + //
        "</div>";

Document doc = Jsoup.parse(html);
Element p = doc.select("p").first();

if (p!=null) {
    System.out.println(p.ownText());
} 

OUTPUT

Earth

Upvotes: 4

Related Questions