Latest web development tutorials

XML DOM - list of nodes and node named Figure

Node list returned by getElementsByTagName () method and childNodes property.


Examples

Try - Example

The following example uses XML files the Books.xml .
Function loadXMLDoc () , in an external JavaScript is used to load the XML file.

Get the text from the first <title> element
This example uses the getElementsByTagName () method to get the text from "books.xml" the first <title> element.

Using length attribute node traversal
This example uses node list and the length property to loop "books.xml" all the <title> element.

Get element attributes
In this example the use of property to get a list of property from "books.xml" the first <book> element.


DOM node list (Node List)

When using such childNodes or getElementsByTagName () property or method that returns a node list object.

Node list object represents a list of nodes, and XML in the same order.

Node list of nodes from the zero-based index number for access.

The following image represents " the Books.xml list of nodes" in the <title> element:

DOM node list

The following code fragment using loadXMLDoc () to " the Books.xml " into xmlDoc and returns "books.xml" node list title element:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title");

After the above statement is executed, x is a node object list.

The following code fragment returns the text from the list of nodes (x) in the first <title> element:

Examples

txt=x[0].childNodes[0].nodeValue;

try it"

After the above statement is executed, txt = "Everyday Italian".


Node list length (Node List Length)

Node list object keeps itself updated. If you delete or add elements, the list is automatically updated.

length attribute node list is the number of nodes in the list.

The following code fragment using loadXMLDoc () to " the Books.xml " into xmlDoc, and returns the number in "books.xml" <title> element:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('title').length;

After the above statement is executed, x = 4.

The length of the node list can be used to traverse the list of all the elements.

The following code fragment uses the length property to loop through the list of <title> element:

Examples

xmlDoc=loadXMLDoc("books.xml");

//the x variable will hold a node list
x=xmlDoc.getElementsByTagName('title');

for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write("
");
}

Output:

Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML

try it"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. Set the variable x to save all the elements of a list of nodes title
  3. Output from the text node values ​​of all <title> element

DOM attribute list (named node graph Named Node Map)

attributes attribute element node returns a list of attribute nodes.

This is called a node named Figure (Named Node Map), except for some differences in the methods and properties, which is similar to the node list.

Property list keeps itself updated. If you delete or add a property, this list is automatically updated.

The following code fragment using loadXMLDoc () to " the Books.xml " into xmlDoc and returns a list of attribute nodes "books.xml" the first <book> element:

xmlDoc=loadXMLDoc("books.xml");

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

After the above code is executed, x.length equal to the number of attributes, you can use x.getNamedItem () returns the attribute node.

The following code fragment shows the number of values ​​"category" attribute of a book, as well as its properties:

Examples

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"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. A list of variable x is set to save all properties of the first <book> element of
  3. From the "category" attribute value output
  4. Length of the output attribute list