Dom Nodes Access
* * *
With DOM, you can access every node in an XML document.
* * *
]( located in an external JavaScript file, is used to load the XML file.
(
This example uses the getElementsByTagName() method to retrieve the third `` element in "books.xml".
(
This example uses the length property to traverse all `` elements in "books.xml".
(
This example uses the nodeType property to obtain the node type of the root element in "books.xml".
(
This example uses the nodeType property to process element nodes in "books.xml".
(
This example uses the nodeType property and the nextSibling property to process element nodes in "books.xml".
* * *
## Accessing Nodes
You can access nodes in three ways:
1. By using the getElementsByTagName() method.
2. By looping (traversing) the node tree.
3. By navigating the node tree using node relationships.
* * *
## The getElementsByTagName() Method
getElementsByTagName() returns all elements with the specified tag name.
### Syntax
_node_.getElementsByTagName(_"tagname"_);
### Example
The following example returns all `` elements under the x element:
x.getElementsByTagName("title");
Note that the above example returns only `` elements under the x node. To return all `` elements in the XML document, use:
xmlDoc.getElementsByTagName("title");
Here, xmlDoc is the document itself (the document node).
* * *
## DOM Node List
The getElementsByTagName() method returns a node list. A node list is an array of nodes.
The following code uses [loadXMLDoc()]( to load "[books.xml](" into xmlDoc, then stores a list of `` nodes in variable x:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
You can access `` elements in x using index numbers. To access the third ``, you can write:
y=x;
**Note:** Indexing starts from 0.
In later chapters of this tutorial, you will learn more about node lists.
* * *
## DOM Node List Length
The length property defines the length (i.e., number of nodes) of a node list.
You can traverse a node list using the length property:
## Example
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title"); for(i=0;i<x.length;i++){document.write(x.childNodes.nodeValue); document.write(""); }
[Try it yourself Β»](
Example explanation:
1. Load "[books.xml]( into xmlDoc using [loadXMLDoc()](
2. Retrieve all `` element nodes
3. Output the value of the text node of each `` element
* * *
## Node Types
The **documentElement** property of an 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.
You will learn more about node properties in the next chapter of this tutorial.
(
* * *
## Traversing Nodes
The following code traverses the child nodes of the root node, which are also element nodes:
## Example
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.documentElement.childNodes; for(i=0;i<x.length;i++){if(x.nodeType==1){document.write(x.nodeName); document.write(""); }}
[Try it yourself Β»](
Example explanation:
1. Load "[books.xml]( into xmlDoc using [loadXMLDoc()](
2. Retrieve the child nodes of the root element
3. Check the node type of each child node. If the node type is "1", it is an element node
4. If it is an element node, output the node name
* * *
## Navigating Node Relationships
The following code navigates the node tree using node relationships:
## Example
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book").childNodes; y=xmlDoc.getElementsByTagName("book").firstChild; for(i=0;i<x.length;i++){if(y.nodeType==1){document.write(y.nodeName + ""); }y=y.nextSibling; }
[Try it yourself Β»](
1. Load "[books.xml]( into xmlDoc using [loadXMLDoc()](
2. Retrieve the child nodes of the first book element
3. Set the variable "y" to the first child node of the first book element
4. For each child node (starting from "y"), check the node type; if the node type is "1", it is an element node
5. If it is an element node, output the node name
6. Set the variable "y" to the next sibling node and run the loop again
YouTip