How can we get previous and next sibling nodes

Having trouble installing Oxygen? Got a bug to report? Post it all here.
SmitaPatil
Posts: 93
Joined: Mon Aug 08, 2022 2:32 pm

How can we get previous and next sibling nodes

Post 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
image.png (41.31 KiB) Viewed 413 times
Please let me know, if there any more information needed.

Thanks & Regards,
Smita
Bogdan Dumitru
Site Admin
Posts: 142
Joined: Tue Mar 20, 2018 5:28 pm

Re: How can we get previous and next sibling nodes

Post 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;
}
Bogdan Dumitru
http://www.oxygenxml.com
Post Reply