Getting an element without attributes using Jsoup

I have the following html using Jsoup. I am trying to extract text in a section p that has no attributes (text “Some text 2”, not “Some text 1”).

<div id="intro"> <h1 class="some class"> <p id="some_id"> Some text 1 </p> <p> Some text 2 </p> </div> 

I tried using the following Jsoup expression:

 div[id=intro] > p:not(:has(@*)) 

But that will not work. Thank you for your help.

+6
source share
1 answer

I think you can use the JSOUP CSS selector p:not([^]) , which will select any p that does not match an attribute starting with something,

 String html = "<div id=\"intro\">" + "<h1 class=\"some class\">" + "<p id=\"some_id\">" + "Some text 1" + "</p>" + "<p name=\"some_name\">" + "Some text A" + "</p>" + "<p data>" + "Some text B" + "</p>" +"<p>" + "Some text 2" +"</p>" +"</div> "; Document doc = Jsoup.parse(html); Elements els = doc.select("p:not([^])"); for (Element el:els){ System.out.println(el.text()); } 

the above example will only print

 Some text 2 

because only this p element has no attributes.

Note that the p[^] selector will select all p elements that have an attribute.

+3
source

All Articles