XML DOM item() Method
NodeList object
Definition and Usage
The item() method returns the node with the specified index in a node list.
Syntax
item(index)
| Parameters | 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 example below, we 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 that node is an element node. In this way, we can get the correct result in all browsers.
**Tip:** For more about browser differences, see our section in the XML DOM tutorial DOM Browsers section.
Example
The following code snippet uses loadXMLDoc() to load "books.xml" load into xmlDoc, and loop through
Example
xmlDoc
=
loadXMLDoc
(
"
books.xml
"
)
;
x
=
xmlDoc
.
documentElement
.
childNodes
;
for
(
i
=
0
;
i
<
x
.
length
;
i
++
)
{
//
Display the element node name
if
(
x
.
item
(
i
)
.
nodeType
==
1
)
{
document
.
write
(
x
.
item
(
i
)
.
nodeName
)
;
document
.
write
(
"
"
)
;
}
}
Output:
book
book
book
book
Try it »
NodeList object