Skip to content

Is there an easy way to exclude the current element from selectors? #2251

Answered by jhy
midgleyc asked this question in Q&A
Discussion options

You must be logged in to vote

Hi,

There are a few ways to do this. The cleanest syntax might be to select on the element's children():

String html = "<div id=1>Outer <div id=2>One</div><div id=3>Two</div></div>";
Document doc = Jsoup.parse(html);
Element el = doc.expectFirst("div");
Elements inner = el.children().select("div");

Or, modify the selector to only match the children directly:

Elements inner = doc.select("div div");

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@midgleyc
Comment options

Answer selected by midgleyc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants