Select 元素但取消选择元素的指定子元素

Select element but deselect specified child of element

只是好奇这在 jsoup 中是否可行

Select 一个元素及其文本,但取消选择指定的子项。

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

是否可以 return 只是 Earth

编辑:此外,在该段落中,位置是静态的,但地球不是。 即你可以

Location: Earth
Location: LA
Location: Mars

您想使用 Element::ownText 方法。

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());
} 

输出

Earth