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

XML DOM – Node Lists and Named Node Maps

XML DOM Node Lists


Node lists are returned by the getElementsByTagName() method and the childNodes property.


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.

From the first get the text of an element</a><br /> This example uses the getElementsByTagName() method to get the first element in "books.xml" <title> get the text of an element.</p> <p><a href="/try/try.php?filename=try_dom_nodelist_length">Use the length property to traverse nodes</a><br /> This example uses the node list and the length property to iterate through all the nodes in "books.xml" <title> element.</p> <p><a href="/try/try.php?filename=try_dom_attributes">Get the attributes of an element</a><br /> This example uses the attribute list from the first element in "books.xml" <book> get an attribute of an element.</p> <hr /> <h2>DOM Node List (Node List)</h2> <p>when using properties or methods such as childNodes or getElementsByTagName(), a node list object is returned.</p> <p>A node list object represents a list of nodes, in the same order as in the XML.</p> <p>Nodes in a node list are accessed using index numbers starting from 0.</p> <p>The following image shows "<a href="/try/demo_source/books.xml">books.xml</a>" in <title> the node list of the element:</p> <p><img src="https://www.runoob.com/wp-content/uploads/2013/09/dom-nodes-nodelist-2020-11-23.png" alt="DOM node list" /></p> <p>The following code snippet by using <a href="dom-loadxmldoc.html">loadXMLDoc()</a> Put the "<a href="/try/demo_source/books.xml">books.xml</a>" loaded into xmlDoc, and returns a node list of title elements in "books.xml":</p> <p>xmlDoc=loadXMLDoc("books.xml");</p> <p>x=xmlDoc.getElementsByTagName("title");</p> <p>After the above statement is executed, x is a node list object.</p> <p>The following code snippet gets the first from the node list (x) <title> return the text of an element:</p> <h2>Example</h2> <p>txt</p> <p>=</p> <p>x</p> <p>[</p> <p>0</p> <p>]</p> <p>.</p> <p>childNodes</p> <p>[</p> <p>0</p> <p>]</p> <p>.</p> <p>nodeValue</p> <p>;</p> <p>Try it »</p> <p>After the above statement is executed, txt = "Everyday Italian".</p> <hr /> <h2>Node List Length (Node List Length)</h2> <p>A node list object keeps itself updated. If elements are removed or added, the list updates automatically.</p> <p>The length property of a node list is the number of nodes in the list.</p> <p>The following code snippet by using <a href="dom-loadxmldoc.html">loadXMLDoc()</a> Put the "<a href="/try/demo_source/books.xml">books.xml</a>" loaded into xmlDoc, and returns in "books.xml" <title> the number of elements:</p> <p>xmlDoc=loadXMLDoc("books.xml");</p> <p>x=xmlDoc.getElementsByTagName('title').length;</p> <p>After the above statement is executed, x = 4.</p> <p>The length of a node list can be used to iterate over all the elements in the list.</p> <p>The following code snippet uses the length property to traverse <title> the list of elements:</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>//</p> <p>The x variable gets the specified node list</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>Output:</p> <p>Everyday Italian</p> <p>Harry Potter</p> <p>XQuery Kick Start</p> <p>Learning XML</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>

  • Set the x variable to hold the node list of all title elements
  • From all output the value of the element's text node</li> </ol>

    DOM Attribute Lists (Named Node Map)

    The attributes property of an element node returns a list of attribute nodes.

    This is called a Named Node Map; apart from some differences in methods and properties, it is similar to a node list.

    The attribute list keeps itself updated. If an attribute is deleted or added, the list is automatically updated.

    The following code snippet uses loadXMLDoc() to load "books.xml" load into xmlDoc, and return the first element in "books.xml" element's attribute node list:

    xmlDoc=loadXMLDoc("books.xml");

    x=xmlDoc.getElementsByTagName('book')[0].attributes;

    After the code above is executed, x.length equals the number of attributes, and x.getNamedItem() can be used to return an attribute node.

    The following code snippet shows the value of a book's "category" attribute, as well as the number of its attributes:

    Example

    xmlDoc

    =

    loadXMLDoc

    (

    "

    books.xml

    "

    )

    ;

    x

    =

    xmlDoc

    .

    getElementsByTagName

    (

    "

    book

    "

    )

    [

    0

    ]

    .

    attributes

    ;

    document

    .

    write

    (

    x

    .

    getNamedItem

    (

    "

    category

    "

    )

    .

    nodeValue

    )

    ;

    document

    .

    write

    (

    "

    "

    x

    .

    length

    )

    ;

    Output:

    cooking

    1

    Try it »

    Example explanation:

    1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
    2. Set the x variable to hold the first list of all attributes of the element
    3. Output the value from the "category" attribute
    4. Output the length of the attribute list