您可以调用getElementsByTag()
任何元素以获取给定类型的所有后代元素(element.getElementsByTag("h")
将获取全部<h>
),然后您可以使用简单的 for 遍历结果以获取顺序。
请参阅示例代码:
import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.*;
public class JsoupHtmlSiblingsOrder {
public static void main(String[] args) {
String html = "<html><body><span>HELLO!</span><table id=\"myTable\"><tbody> " +
"<tr><td> <span> " +
" ..... " +
" <h>apple</h> " +
" ..... " +
" </span> " +
"</td><td><span> " +
" ..... " +
" <h>orange</h> " +
" ..... " +
" </span> " +
"</td><td><span> " +
" ..... " +
" <h>pear</h> " +
" ..... " +
" </span> " +
"</td></tr> " +
"</tbody></table></body></html> ";
Document doc = Jsoup.parse(html);
Element table = doc.getElementById("myTable");;
Elements hs = table.getElementsByTag("h");
for (int i = 0; i < hs.size(); i++) {
Element h = hs.get(i);
System.out.println(h.text()+":"+(i+1));
}
}
}
输出:
apple:1
orange:2
pear:3