XML DOM: Accessing Node with getElementsByTagName() Method
The getElementsByTagName()
Method
getElementsByTagName()
JavaScript method returns all elements with a specified tag name.
Syntax
The getElementByTagName()
method has the following syntax:
Syntax
node.getElementsByTagName("tagname");
where:
node
is the document node.tagname
holds the name of the node whose value you want to get.
DOM Node List
The getElementsByTagName()
method returns a node list. A node list is an array of nodes.
For example:
x = xmlDoc.getElementsByTagName("title");
The <title>
elements in x can be accessed by index number. To access the third <title>
you can write:
y = x[2];
note
xmlDoc
is the document itself (document node).
DOM Node List Length
The length property defines the length of a node list (the number of nodes).
You can loop through a node list by using the length property:
var x = xmlDoc.getElementsByTagName("title");
for (i = 0; i <x.length; i++) {
// do something for each node
}
Node Types
The documentElement
property of the XML document is the root node.
The nodeName
property of a node is the name of the node.
The nodeType
property of a node is the type of the node.