Accessing Nodes in XML DOM
- Previous Page DOM Node
- Next Page DOM Node Information
Through DOM, you can access each node in the XML document.
Accessing Nodes
There are three ways to access a node:
- By using the getElementsByTagName() method
- By traversing the node tree
- By utilizing the relationship between nodes to navigate in the node tree
getElementsByTagName() method
getElementsByTagName()
Returns all elements with the specified tag name.
Syntax
node.getElementsByTagName("tagname");
Example
The following example returns all <title> elements under the x element:
x.getElementsByTagName("title");
Note that the above example only returns the <title> elements under the x node. To return all <title> elements in the XML document, please use:
xmlDoc.getElementsByTagName("title");
Here,xmlDoc
is the document itself (the document node).
List of DOM nodes
getElementsByTagName()
The method returns a node list (node list). The node list is an array of nodes.
x = xmlDoc.getElementsByTagName("title");
You can access the <title> element in x by index number. If you need to access the third <title>, you can write it like this:
y = x[2];
Note:The index starts from 0.
DOM node list length
length
Attribute to define the length of the node list (number of nodes).
You can use length
Attribute to loop through the node list:
Example
var x = xmlDoc.getElementsByTagName("title"); for (i = 0; i < x.length; i++) { // You can write code to process each node here }
Node type
The nodes of the XML document documentElement
The attribute is the root node.
The attribute is the node's nodeName
The attribute is the node's name.
The attribute is the node's nodeType
Attributes are the type of the node.
You will learn more about node attributes in the next chapter of this tutorial.
Traversal of nodes
The following code loops through the child nodes of the root node (which are also element nodes):
Example
txt = ""; x = xmlDoc.documentElement.childNodes; for (i = 0; i < x.length; i++) { // Process only element nodes (type 1) if (x[i].nodeType == 1) { txt += x[i].nodeName + "<br>"; } }
Example explanation:
- Assuming you have already loaded "books.xml"Load into
xmlDoc
- Get the child nodes of the root element (xmlDoc)
- Check the node type of each child node. If the node type is "
1
", it is an element node - If it is an element node, then output the node name
Navigation node relationships
The following code navigates through the node tree by utilizing the relationships between nodes:
Example
x = xmlDoc.getElementsByTagName("book")[0]; xlen = x.childNodes.length; y = x.firstChild; txt = ""; for (i = 0; i < xlen; i++) { // Only handle element nodes (type 1) if (y.nodeType == 1) { txt += y.nodeName + "<br>"; } y = y.nextSibling; }
Example explanation:
- Assuming you have already loaded "books.xml"Load into
xmlDoc
- Get the child nodes of the first 'book' element
- Set the "y" variable to the first child node of the first book element
- For each child node (from the first child node "
y
" start): - Check the node type of each child node. If the node type is "
1
" it is an element node - If it is an element node, then output the name of the node
- Set "
y
" Variable is set to the next sibling node, and the loop is run again
- Previous Page DOM Node
- Next Page DOM Node Information