XML DOM node access
By DOM, you can access each node in the XML document.
Try - Example
The following example uses XML files the Books.xml .
Function loadXMLDoc () , in an external JavaScript is used to load the XML file.
Use the node list index number to access node
This example uses getElementsByTagname () method to get "books.xml" the third <title> element.
Use the length property to loop through a node
This example uses the length property to loop through "books.xml" all the <title> element.
View node type element
This example uses the nodeType property to get "books.xml" node type of the root element.
Traversing element node
This example uses the nodeType property to deal with "books.xml" element nodes.
Relationship using the node to traverse the element node
This example uses the nodeType property and nextSibling property to handle "books.xml" element nodes.
Access Node
You can access nodes in three ways:
1. By using the getElementsByTagName () method.
2. Cycle (traversing) node tree.
3. By using the node relationships in the node navigation tree.
getElementsByTagName () method
getElementsByTagName () returns all the elements have the specified tag name.
grammar
Examples
The following example returns all <title> element under the element x:
Please note that the above example returns only the <title> element x node. To return the XML document all <title> elements, use:
Here, xmlDoc is the document itself (document node).
DOM node list (Node List)
getElementsByTagName () method returns a list of nodes. Node list is an array of nodes.
The following code uses loadXMLDoc () to " the Books.xml " into xmlDoc, then a list of stored <title> node in the variable x:
x=xmlDoc.getElementsByTagName("title");
Access by index number x in the <title> element. To access the third <title>, you can write:
NOTE: The index starts at 0.
In a later chapter of this tutorial, you will learn more about the list of nodes (Node List) knowledge.
DOM node list length (Node List Length)
Length length property defines the node list (ie, the number of nodes).
You can use the length property to loop through a list of nodes:
Examples
x=xmlDoc.getElementsByTagName("title");
for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write("
");
}
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- Get all <title> element node
- Each output value <title> element of the text node
Node Type (Node Types)
DocumentElement attribute of an XML document is the root node.
NodeName attribute node is the name of the node.
NodeType attribute node is the type of node.
You will learn more about the properties of the nodes in the next chapter of this tutorial.
Traversing the node
The following code iterates through the root of the child nodes, but also the element node:
Examples
x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++)
{
if (x[i].nodeType==1)
{//Process only element nodes (type 1)
document.write(x[i].nodeName);
document.write("
");
}
}
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- Getting child nodes of the root element
- Node type check each child node. If the node type is "1", the node is an element
- If the node is an element, the name of the output node
Relations navigation node
The following code uses the navigation node tree node relationship:
Examples
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
y=xmlDoc.getElementsByTagName("book")[0].firstChild;
for (i=0;i<x.length;i++)
{
if (y.nodeType==1)
{//Process only element nodes (type 1)
document.write(y.nodeName + "
");
}
y=y.nextSibling;
}
try it"
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- Get the first child node of a book element
- The "y" variable is set to the first element of a book first child
- For each child node (from the first child node "y" began), the type of the check node, if the node type is "1", the node is an element
- If the node is an element, the name of the output node
- The "y" variable is set to the next sibling node, and run the cycle again