简体   繁体   English

如何使用jsoup比较DOM中元素的子级

[英]How to compare children of an element in a DOM with jsoup

I am working on a project where I have to be able to know that an element have repeated children .For example in that DOM, I want to know that the element tbody has similar children 我在一个项目中必须知道一个元素有重复的子代,例如在那个DOM中,我想知道元素tbody具有相似的子代

DOM范例

My goal is to extract data- and store it in a database -from pages that I ignore their structure. 我的目标是从我忽略其结构的页面中提取数据并将其存储在数据库中。

使用jQuery的让你的td元素,并遍历每个他们。

you can use JSOUP for this. 您可以为此使用JSOUP its very easy to use as well 它也非常易于使用

for example you want to get all td tag in within your document: 例如,您要在文档中获取所有td标签:

String html=... //your html string
Document doc = JSoup.parse(html);
Elements elements = doc.select("tbody").select("td");
System.out.println(elements.size()); //prints number of td within tbody REGARDLESS of where in the DOM tree they live. 

Edit1: EDIT1:

to get all elements you can do: 获得所有您可以做的元素:

for(Element e : doc.getAllElements){
  System.out.println(e.getTagName());//prints the tag name
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM