XML DOM nextSibling property
Definition and Usage
The nextSibling property returns the next sibling node of the selected node (the next node at the same tree level).
If no such node exists, this property returns NULL.
Syntax:
elementNode.nextSibling
Tips and comments
Note:Internet Explorer ignores the whitespace text nodes generated between nodes (such as newline characters), while Mozilla does not. Therefore, in the following example, we use a function to detect the node type of the next sibling node.
The node type of an element node is 1, so if the next sibling node is not an element node, move to the next node and check if the node is an element node. This process continues until the next sibling node is found. This method ensures that the correct results are obtained in both Internet Explorer and Mozilla.
For more information about the differences between IE and Mozilla browsers, please visit the XML DOM tutorial on CodeW3C.com DOM Browser in this section.
Example
In all examples, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().
The following code snippet retrieves the next sibling node of a <title> element in an XML document:
//check if the next sibling node is an element node
function get_nextsibling(n)
{
var x=n.nextSibling;
while (x.nodeType!=1)
{
x=x.nextSibling
;
}
return x;
}
xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("title")[0];
document.write(x.nodeName);
document.write(" = ");
document.write(x.childNodes[0].nodeValue);
var y=get_nextsibling(x);
document.write("<br />Next sibling: ");
document.write(y.nodeName);
document.write(" = ");
document.write(y.childNodes[0].nodeValue);
Rufin gida na cikin:
title = Everyday Italian Next sibling: author = Giada De Laurentiis