Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSoup parsing: get next element

I have searched around I cannot find a way to do this. I have a group of elements. I select one element from this group. How could I find the next element after this selected element?

like image 659
superuser Avatar asked Oct 16 '13 00:10

superuser


1 Answers

Use the method nextElementSibling().

Example:

<body>
    <p>First in family!</p>
    <p>Second in family!</p>
    <p>Third in family!</p>
</body>

Jsoup:

Element firstParagraph = doc.select("p:eq(0)").first();
Element secondParagraph = firstParagraph.nextElementSibling();

System.out.println(firstParagraph.text() + " " + secondParagraph.text());

Output: First in family! Second in family!

like image 74
Daniel B Avatar answered Nov 03 '22 00:11

Daniel B