Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML DOM Accessing Nodes

XML DOM - Accessing Nodes


Through the DOM, you can access every node in an XML document.


Try it - Example

The following example uses the XML file books.xml.
function loadXMLDoc(), located in an external JavaScript, used to load the XML file.

Access nodes using their index in the node list
This example uses the getElementsByTagname() method to get the third element in "books.xml" element.</p> <p><a href="/try/try.php?filename=try_dom_list_loop">Use the length property to traverse the nodes</a><br /> This example uses the length property to iterate through all the elements in "books.xml" <title> element.</p> <p><a href="/try/try.php?filename=try_dom_root">View the node type of an element</a><br /> This example uses the nodeType property to get the node type of the root element in "books.xml".</p> <p><a href="/try/try.php?filename=try_dom_loop">Traverse element nodes</a><br /> This example uses the nodeType property to handle the element nodes in "books.xml".</p> <p><a href="/try/try.php?filename=try_dom_navigate">Use the relationships between nodes to traverse element nodes</a><br /> This example uses the nodeType property and the nextSibling property to handle the element nodes in "books.xml".</p> <hr /> <h2>Access nodes</h2> <p>You can access nodes in three ways:</p> <ol> <li> <p>By using the getElementsByTagName() method.</p> </li> <li> <p>By looping (traversing) through the node tree.</p> </li> <li> <p>Navigate in the node tree by using the relationships between nodes.</p> </li> </ol> <hr /> <h2>getElementsByTagName() Method</h2> <p>getElementsByTagName() returns all elements with the specified tag name.</p> <h3>Syntax</h3> <p>node</p> <p>.getElementsByTagName(</p> <p>"tagname"</p> <p>);</p> <h3>Example</h3> <p>The following example returns all the elements under the x element <title> element:</p> <p>x.getElementsByTagName("title");</p> <p>Please note that the example above only returns the ones under node x <title> element. To return all the elements in the XML document <title> element, please use:</p> <p>xmlDoc.getElementsByTagName("title");</p> <p>Here, xmlDoc is the document itself (the document node).</p> <hr /> <h2>DOM Node List (Node List)</h2> <p>The getElementsByTagName() method returns a node list. A node list is an array of nodes.</p> <p>The following code uses <a href="dom-loadxmldoc.html">loadXMLDoc()</a> Put the "<a href="/try/demo_source/books.xml">books.xml</a>" loaded into xmlDoc, then stored in variable x <title> A list of nodes:</p> <p>xmlDoc=loadXMLDoc("books.xml");</p> <p>x=xmlDoc.getElementsByTagName("title");</p> <p>can be accessed by index number in x <title> element. To access the third <title>, you can write:</p> <p>y=x[2];</p> <p>**Note:** The index starts from 0.</p> <p>In later chapters of this tutorial, you will learn more about node lists (Node List).</p> <hr /> <h2>DOM Node List Length (Node List Length)</h2> <p>The length property defines the length of a node list (that is, the number of nodes).</p> <p>You can iterate through a node list by using the length property:</p> <h2>Example</h2> <p>xmlDoc</p> <p>=</p> <p>loadXMLDoc</p> <p>(</p> <p>"</p> <p>books.xml</p> <p>"</p> <p>)</p> <p>;</p> <p>x</p> <p>=</p> <p>xmlDoc</p> <p>.</p> <p>getElementsByTagName</p> <p>(</p> <p>"</p> <p>title</p> <p>"</p> <p>)</p> <p>;</p> <p>for</p> <p>(</p> <p>i</p> <p>=</p> <p>0</p> <p>;</p> <p>i</p> <p><</p> <p>x</p> <p>.</p> <p>length</p> <p>;</p> <p>i</p> <p>++</p> <p>)</p> <p>{</p> <p>document</p> <p>.</p> <p>write</p> <p>(</p> <p>x</p> <p>[</p> <p>i</p> <p>]</p> <p>.</p> <p>childNodes</p> <p>[</p> <p>0</p> <p>]</p> <p>.</p> <p>nodeValue</p> <p>)</p> <p>;</p> <p>document</p> <p>.</p> <p>write</p> <p>(</p> <p>"</p> <p>"</p> <p>)</p> <p>;</p> <p>}</p> <p>Try it »</p> <p>Example explained:</p> <ol> <li>Use <a href="dom-loadxmldoc.html">loadXMLDoc()</a> Put the "<a href="/try/demo_source/books.xml">books.xml</a>" loaded into xmlDoc</li> <li>Get all <title> element node</li> <li>Output each <title> the value of the element's text node</li> </ol> <hr /> <h2>Node Types (Node Types)</h2> <p>of an XML document <strong>documentElement</strong> the attribute is the root node.</p> <p>The node's <strong>nodeName</strong> the attribute is the name of the node.</p> <p>The node's <strong>nodeType</strong> the attribute is the type of the node.</p> <p>You will learn more about node properties in the next chapter of this tutorial.</p> <p><a href="/try/try.php?filename=try_dom_root">Try it</a></p> <hr /> <h2>Traverse nodes</h2> <p>The following code traverses the child nodes of the root node that are also element nodes:</p> <h2>Example</h2> <p>xmlDoc</p> <p>=</p> <p>loadXMLDoc</p> <p>(</p> <p>"</p> <p>books.xml</p> <p>"</p> <p>)</p> <p>;</p> <p>x</p> <p>=</p> <p>xmlDoc</p> <p>.</p> <p>documentElement</p> <p>.</p> <p>childNodes</p> <p>;</p> <p>for</p> <p>(</p> <p>i</p> <p>=</p> <p>0</p> <p>;</p> <p>i</p> <p><</p> <p>x</p> <p>.</p> <p>length</p> <p>;</p> <p>i</p> <p>++</p> <p>)</p> <p>{</p> <p>if</p> <p>(</p> <p>x</p> <p>[</p> <p>i</p> <p>]</p> <p>.</p> <p>nodeType</p> <p>==</p> <p>1</p> <p>)</p> <p>{</p> <p>//</p> <p>Execute once</p> <p>document</p> <p>.</p> <p>write</p> <p>(</p> <p>x</p> <p>[</p> <p>i</p> <p>]</p> <p>.</p> <p>nodeName</p> <p>)</p> <p>;</p> <p>document</p> <p>.</p> <p>write</p>

(

"

"

)

;

}

}

Try it »

Example explanation:

  1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
  2. Get 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's name

Navigate node relationships

The following code uses node relationships to navigate the node tree:

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 the node name

document

.

write

(

y

.

nodeName

"

"

)

;

}

y

=

y

.

nextSibling

;

}

Try it »

  1. Use loadXMLDoc() to load "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 from "y" for the first child node), 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's name
  6. Set the "y" variable to the next sibling node, and run the loop again