开发者

How can I select the divs elements that not having another divs inside it?

开发者 https://www.devze.com 2023-03-28 15:53 出处:网络
I\'m using Java and Jsoup to parse HTML pages and I want to get all the divs that not contains another divs inside it to print the text it contains.

I'm using Java and Jsoup to parse HTML pages and I want to get all the divs that not contains another divs inside it to print the text it contains.

But for example, if a div contains a table, and the table costains a div, I don't want it. I want onl开发者_StackOverflow中文版y the div at the most bottom level, with none another div inside it (another tags are ok).

How I do this?

Primarilly, I want to know if there is some syntax that can I use with the select() method.


Document doc; //comes as parameter

Elements divs = doc.getElementsByTag("div");
for(Element div: divs){
    if(div.getElementsByTag("div").size() == 1){
        //is a div with no divs inside it
    }
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消