简体   繁体   中英

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范例

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. its very easy to use as well

for example you want to get all td tag in within your document:

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:

to get all elements you can do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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