I want to parse this with Jsoup (this is a simplification, I would be parsing entire web pages)
<html><body><p>A<strong>B</strong>C<strong>D</strong>E</p></body></html>
to obtain all text elements in the order they appear, this is:
A B C D E
I have tried two approaches:
Elements elements = doc.children().select("*");
for (Element el : elements)
System.out.println(el.ownText());
which returns:
A C E B D
This is, the elements between "strong" tags go at the end.
I have also tried a recursive version:
myfunction(doc.children());
private void myfunction(Elements elements) {
for (Element el : elements){
List<Node> nodos = el.childNodes();
for (Node nodo : nodos) {
if (nodo instanceof TextNode && !((TextNode) nodo).isBlank()) {
System.out.println(((TextNode) nodo).text());
}
}
myfunction(el.children());
}
But the result is the same as before.
How can this be accomplished? I feel I am making difficult something simple ...