Getting element text without including subitem text using jsoup

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

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

I want to get li NOT text, including span text. So I want to get a "Category Name" without an "X". (If I call the method text()on the li element, I get an "XCategory Name".) How can I exclude a subrange?

+1
source share
1 answer

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

All Articles