3

I'm 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 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?

BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
froadie
  • 75,789
  • 72
  • 163
  • 232

1 Answers1

4

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());
}
Ken de Guzman
  • 2,742
  • 1
  • 17
  • 31