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

node.getElementsByTagName("tagname");

Example

The following example returns all <title> elements under the x element:

x.getElementsByTagName("title");

Note that the example above only returns the <title> elements under the x node. To return all <title> 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. 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:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title");

You can access the <title> elements in x by index number. To access the third <title>, you can write:

y=x[2];

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

xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title"); for (i=0;i<x.length;i++) { document.write(x[i].childNodes[0].nodeValue); document.write(""); }

Try it Yourself »

Example explanation:

  1. UsageloadXMLDoc()put "books.xml" load into xmlDoc
  2. Get all <title> element nodes
  3. 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.

Try it Yourself


Traverse Node

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[i].nodeType==1) { //Execute once document.write(x[i].nodeName); document.write(""); } }

Try it Yourself »

Example explanation:

  1. UsageloadXMLDoc()put "books.xml" load into xmlDoc
  2. Getting 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 name of the node.

Navigating node relationships

The following code navigates the node tree using node relationships:

Example

xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0].childNodes; y=xmlDoc.getElementsByTagName("book")[0].firstChild; for (i=0;i<x.length;i++) { if (y.nodeType==1) { //Output node name document.write(y.nodeName + ""); } y=y.nextSibling; }

Try it Yourself »
  1. UsageloadXMLDoc()put "books.xml" load into xmlDoc
  2. Get the child nodes of the first book element.
  3. Set the "y" variable to the first child node of the first book element.
  4. 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.
  5. If it is an element node, output the name of that node.
  6. Set the "y" variable to the next sibling node, and run the loop again.

other extensions