Page 1 of 1

How can we get previous and next sibling nodes

Posted: Wed Nov 30, 2022 1:15 pm
by SmitaPatil
Hi Team,
Can you please tell how can we get previous and next sibling of current selected node?
We you see in below screenshot we have some nodes in document, suppose currently we have access to node which has content pub target.
Can we get the next sibling node(which has content abc) and previous sibling node(which has content pub/link).
image.png
Please let me know, if there any more information needed.

Thanks & Regards,
Smita

Re: How can we get previous and next sibling nodes

Posted: Wed Nov 30, 2022 3:50 pm
by Bogdan Dumitru
Hello,

For this, you can inspect the content nodes of the parent element, somehow like this:
AuthorElement parentElement = ((AuthorElement)currentNode.getParent());
List<AuthorNode> allSiblings = parentElement.getContentNodes();
int currentNodeIndex = allSiblings.indexOf(currentNode);

AuthorNode previousSibling;
if (currentNodeIndex - 1 >= 0) {
previousSibling = allSiblings.get(currentNodeIndex - 1);
} else {
previousSibling = null;
}

AuthorNode nextSibling;
if (currentNodeIndex + 1 < allSiblings.size()) {
nextSibling = allSiblings.get(currentNodeIndex + 1);
} else {
nextSibling = null;
}