XML DOM - Access Node
With DOM, you can access every node in an XML document.
Try it Yourself - Example
The following example uses an XML filebooks.xml。
FunctionloadXMLDoc(), located in external JavaScript, is used to load the XML file.
Use the index number in the node list to access nodes
This example uses the getElementsByTagname() method to retrieve the third <title> element in "books.xml".
Use the length property to traverse nodes.
This example uses the length property to iterate through all <title> elements in "books.xml".
View the element's node type
This example uses the nodeType property to get the node type of the root element in "books.xml".
Traverse Element Nodes
This example uses the nodeType property to handle element nodes in "books.xml".
Use node relationships to traverse element nodes
This example uses the nodeType property and the nextSibling property to process element nodes in "books.xml".
Access Node
You can access nodes in three ways:
1. By using the getElementsByTagName() method.
2. By looping through (traversing) the node tree.
3. By navigating the node tree using the relationships between nodes.
getElementsByTagName() Method
getElementsByTagName() returns all elements with the specified tag name.
Syntax
Example
The following example returns all <title> elements under the x element:
Note that the example above only returns the <title> elements under the x node. To return all <title> elements in the XML document, use:
Here, xmlDoc is the document itself (the document node).
DOM Node List
The getElementsByTagName() method returns a node list. The node list is an array of nodes.
The following code usesloadXMLDoc()put "books.xml"Load into xmlDoc, then store a list of <title> nodes in variable x:
x=xmlDoc.getElementsByTagName("title");
You can access the <title> elements in x by index number. To access the third <title>, you can write:
Note:The index starts at 0.
You will learn more about Node Lists in later chapters of this tutorial.
DOM Node List Length (Node List Length)
The length property defines the length of the node list (that is, the number of nodes).
You can traverse the node list by using the length property:
Example
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" load into xmlDoc
- Get all <title> element nodes
- Output the value of the text node of each <title> element
Node Types
of the XML documentdocumentElementThe attribute is the root node.
node'snodeNameThe nodeName property is the name of a node.
node'snodeTypeThe nodeType property is the type of a node.
You will learn more about node properties in the next chapter of this tutorial.
Traverse Node
The following code traverses the child nodes of the root node, which are also element nodes:
Example
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" load into xmlDoc
- Getting the child nodes of the root element
- 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, output the name of the node.
Navigating node relationships
The following code navigates the node tree using node relationships:
Example
Try it Yourself »
- UsageloadXMLDoc()put "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 (starting with the first child node from "y"), check the node type. If the node type is "1", it is an element node.
- If it is an element node, output the name of that node.
- Set the "y" variable to the next sibling node, and run the loop again.
other extensions