The first element that comes after sibling or descendant?

advertisements

I am trying to always find the first <ul> that comes after <h2>, preferrably without looping, using a single Jsoup select() statement.

For example, that <ul> can be a sibling of <h2> as shown in this HTML snippet:

<!-- lots of things can come before -->

<h2 class="C" id="S3">
<button class="B">Click Me</button>
<span id="targetSpan">Target Span</span>
</h2>

<ul>
<li> List item 1</li>
<li> List item 2</li>
</ul>

<!-- lots of things can come after -->

Or it can be a descendant (not necessarily a direct child!) of a sibling of <h2>. The sibling may or may not be the first sibling element after <h2>, but <ul> is always the first <ul> after that <h2>. For example:

<!-- lots of things can come before -->

<h2 class="C" id="S3">
<button class="B">Click Me</button>
<span id="targetSpan">Target Span</span>
</h2>

<div>
<ul>
<li> List item 1</li>
<li> List item 2</li>
</ul>
</div>

<!-- lots of things can come after -->

I can find the <h2> easily:

Element h2 = select("h2 > span#targetSpan").first().parent();

But once I have h2, how do I find the first <ul> after it? (could be sibling or descendant, I don't control that HTML code)


You can't avoid your own loop. You must iterate through all next element siblings until you find the next <ul>:

  Element h2next = h2.nextElementSibling();
  do {
    ul = h2next.select("ul:not([class]))").first();
  } while (h2next!=null && ul==null);