【发布时间】:2012-07-05 04:01:42
【问题描述】:
我试图始终找到<h2> 之后的第一个 <ul>,最好不要循环,使用单个 Jsoup select() 语句。
例如,<ul> 可以是 <h2> 的兄弟,如以下 HTML sn-p 所示:
<!-- 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 -->
或者它可以是<h2> 的兄弟姐妹的后代(不一定是直系孩子!)。兄弟元素可能是也可能不是<h2> 之后的第一个兄弟元素,但<ul>始终是<h2> 之后的第一个<ul>。例如:
<!-- 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 -->
我可以轻松找到<h2>:
Element h2 = select("h2 > span#targetSpan").first().parent();
但是一旦我有了h2,我如何找到它之后的第一个<ul>? (可能是兄弟或后代,我不控制该 HTML 代码)
【问题讨论】:
标签: jsoup