获取元素的文本,不包含jsoup的子元素的文本

获取元素的文本,不包含jsoup的子元素的文本

问题描述:

我使用jsoup来解析HTML。有以下列表项:

I'm using jsoup to parse HTML. There are list items that look like this:

<li><span class="chk">X</span>Category Name</li>

我想获取li的文本,不包括span的文本。所以我想得到类别名称没有X。 (如果我在li元素上调用 text()方法,我得到XCategory Name。)如何排除子范围?

I want to get the text of the li NOT including the text of the span. So I want to get "Category Name" without the "X". (If I invoke the text() method on the li element, I get "XCategory Name".) How can I exclude the sub-span?

ownText() method will help you here.

Document document = Jsoup.parse("<ul><li><span class=\"chk\">X</span>Home</li><li><spanclass=\"chk\">X</span>Category Name</li></ul>");
Elements elems = document.select("li");
for(Element elem : elems){
    System.out.println(elem.ownText());
}