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
  • 700,868
  • 160
  • 1,392
  • 1,356
froadie
  • 79,995
  • 75
  • 166
  • 235

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,790
  • 1
  • 19
  • 33