XML DOM item() method
Definition and usage
The item() method can return the node at the specified index in the node list.
Syntax
item(index)
Parameter | Description |
---|---|
index | Represent the integer position of the node in the NodeList. This value is an integer greater than or equal to 0 and less than or equal to NodeList.length-1. |
Instance
In all examples, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().
The following code snippet can iterate over all direct child nodes of the <bookstore> element in the XML document:
xmlDoc=loadXMLDoc("/example/xdom/books.xml");
var x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++)
{
//Only display element nodes
if (x.item(i).nodeType==1)
{
document.write(x.item(i).nodeName
)
document.write("<br />")
}
}
Output:
book book book book