I have a org.w3c.dom.Node object.
I would like to see if it has any other siblings.
Here’s what I have tried:
Node sibling = node.getNextSibling();
if(sibling == null)
return true;
else
return false;
HOWEVER, for some reason (possibly due to identation or line spaces in the source XML) I am not getting the expected result.
[Also
node.getParentNode().getChildNodes().getLength()
is giving a value higher higher than I would expect.]
I welcome your suggestions to improve this code.
EDIT
As suggested below it seems that blank nodes are thwarting my attempts to count the siblings.
The xml looks something like this:
<a>
<b>
<c>I have this node:</c>
<c>I want to know how many of these there are.</c>
<c>This is another sibling.</c>
</b>
<b>
</b>
</a>
Starting from my node (the first <c></c> above), how do I find out the number of other siblings?
When you get child nodes from another node, you receive all direct children. This will include Element nodes, Text nodes, and Comment nodes. Most often, you’ll only care about Element nodes. So you can check the result of getChildNodes to see if any are ELEMENT_NODE.
An example function for what you wanted to do:
Just to see what each node type is like:
Getting the div’s childNodes would return 3 nodes, a comment node containing “Comment Node”, an Element node of “P”, and a Text node of “a text node”.