获取元素的文本,不包含jsoup的子元素的文本 [英] Getting text of element without including text of sub-element with jsoup

查看:491
本文介绍了获取元素的文本,不包含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());
}

这篇关于获取元素的文本,不包含jsoup的子元素的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆