How do I select a direct child of "this element" in JSoup -


if have element looks this:

<foo>     <bar> bar text 1 </bar>     <baz>         <bar> bar text 2 </bar>     </baz> </foo> 

and have <foo> element selected, , want select <bar> element direct child of <foo> not 1 child of <baz>, how specify that?

element foo = <that thing above> foo.select("bar").text(); 

yields "bar text 1 bar text 2"

what want

foo.select("this > bar").text(); 

the question is: how specify "this element" in selector?

note desired bar might not first -- need solution work for:

<foo>     <baz>         <bar> bar text 2 </bar>     </baz>     <bar> bar text 1 </bar> </foo> 

use :root structural pseudo-element specify "this element". element.select javadoc, see select uses "this element starting context" , can match "this element, or of children"; is, :root refers element, not actual document root. following code demonstrates placing second example in outer tags:

//nest second sample in fake outer html body element html = (element)parser.parsefragment("<html><body><foo>\n" +                 "    <baz>\n" +                 "        <bar> bar text 2 </bar>\n" +                 "    </baz>\n" +                 "    <bar> bar text 1 </bar>\n" +                 "</foo></body></html>", null, "http://example.com").get(0); element foo = html.select("foo").first();  system.out.println(foo.select(":root > bar")); 

this code prints

<bar>   bar text 1  </bar> 

correctly skipping nested bar element.

according jsoup changelog, structural pseudo-element support added in 1.7.2.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -