With the help of Jsoup evaporate data from a certain parent branch
-
In the Android page, the annex (using the latest version of the lyb from Jsoup.org) selects only the subsidiary elements (all pink elements are selected) and one can first select the daughter elements from the first parent and then only from the second, etc. How do you like it?
-
An obvious solution with regard to the choice of parenting and the subsequent search for children.
public static void main(String[] args) { String html = "<html>" + "<head>" + " <title>Try jsoup</title>" + "</head>" + "<body>" + " <div id=\"parent1\">" + " <p>child1.1</p>" + " <p>child1.2</p>" + " <p>child1.3</p>" + " </div>" + " <div id=\"parent2\">" + " <p>child2.1</p>" + " <p>child2.2</p>" + " <p>child2.3</p>" + " </div>" + " <div id=\"parent3\">" + " <p>child3.1</p>" + " <p>child3.2</p>" + " <p>child3.3</p>" + " </div>" + " <div id=\"parent4\">" + " </div>" + "</body>" + "</html>";
Document doc = Jsoup.parse( html ); Elements parents = doc.select( "div" ); // выбор родителей System.out.printf( // можно выбрать элемент по индексу "parent[%d]: %s#%s%n", 2, parents.get( 2 ).tagName(), parents.get( 2 ).id() ); for ( Element parent : parents ) { System.out.printf( "parent: %s#%s%n", parent.tagName(), parent.id() ); for ( Element child : parent.select( "p" ) ) { // выбор внутри родителя System.out.println( child.text() ); } }
}
He'll find out:
parent[2]: div#parent3
parent: div#parent1
child1.1
child1.2
child1.3
parent: div#parent2
child2.1
child2.2
child2.3
parent: div#parent3
child3.1
child3.2
child3.3
parent: div#parent4
Used http://jsoup.org/apidocs/org/jsoup/select/Selector.html
:has(selector)
You can only choose those.<div>
♪<p>
:Elements parents = doc.select( "div:has(p)" );
Then it won't be extra.
#parent4
♪