XML DOM item()Methods


NodeList object

Definition and Usage

The item() method returns the node at the specified index in the node list.

Syntax

item(index)

Parameter Description
index index.


Tips and notes

Note:Firefox and most other browsers treat empty spaces or line breaks generated between nodes as text nodes, while Internet Explorer ignores blank text nodes generated between nodes. Therefore, in the examples below, we will use a function to check the node type.

The node type of an element node is 1, so if a child node is not an element node, it moves to the next node and continues checking whether this node is an element node. In this way, we can get correct results in all browsers.

Tip:To learn more about browser differences, please visit ourDOM browserChapter.


Example

The following code snippet usesloadXMLDoc()put "books.xmlLoad into xmlDoc, and loop through all child element nodes of the <bookstore> element:

Example

xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.documentElement.childNodes; for (i=0;i<x.length;i++) { //Display element node name if (x.item(i).nodeType==1) { document.write(x.item(i).nodeName); document.write("<br>"); } }

Output:

book
book
book
book

Try it Yourself »

NodeList objectother extensions