davidad davidad - 5 months ago 33
HTML Question

extract element in jsoup in first level, no recursive

I have this html. I need the "li". I use .select("li"), but inside each "li", there may be another "li" but I'm not interested. I only want "li" in first level.
It possible?

<div id="id">
<ul>
<li>
<div>
<ul>
<li> ........ </li>
</ul>
</div>
</li>
<li> ........ </li>
<li> ........ </li>
<li> ........ </li>
<li> ........ </li>
<li> ........ </li>
.
.
.
</ul>
</div>

Answer

Sure it is:

Elements documentElements = document.getElementsByTag("id"); //get the div
Element theList= documentElements.get(0); //get the unordered list
Element listItem = theList.child(0); //this is the first list item in that unordered list

This answer assumes you've already loaded the HTML and have the JSoup Document ready for traversing

Reference: