XML DOM Get Node Value

The nodeValue attribute is used to get the text value of the node.

The getAttribute() method returns the value of the attribute.

Example

The following example uses an XML file books.xml.

Function loadXMLDoc(), located in external JavaScript, used to load XML files.

Get the value of the element
In this example, getElementsByTagname() is used to get the first <title> element in 'books.xml'.
Get the value of the attribute
In this example, the getAttribute() method is used to get the value of the 'lang' attribute of the first <title> element in 'books.xml'.

Get the value of the element

In the DOM, each component is a node. Element nodes do not have text values.

The text of element nodes is stored in child nodes. This node is called a text node.

The method to get the text of an element is to get the value of this child node (text node).

Get element value

The getElementsByTagName() method returns a node list containing all elements with the specified tag name, in the order they appear in the source document.

The following code uses loadXMLDoc() Set "books.xmlLoad xmlDoc and retrieve the first <title> element:

xmlDoc=loadXMLDoc("books.xml");
x = xmlDoc.getElementsByTagName("title")[0];

The childNodes attribute returns a list of child nodes. The <title> element has only one child node, which is a text node.

The following code retrieves the text node of the <title> element:

x = xmlDoc.getElementsByTagName("title")[0];
y = x.childNodes[0];

The nodeValue attribute returns the text value of the text node:

x = xmlDoc.getElementsByTagName("title")[0];
y = x.childNodes[0];
txt = y.nodeValue;

Result: txt = "Harry Potter"

TIY

Traverse all <title> elements:TIY

Get the value of the attribute

In the DOM, attributes are also nodes. Unlike element nodes, attribute nodes have text values.

The method to get the value of an attribute is to get its text value.

This task can be completed by using the getAttribute() method or the nodeValue attribute of the attribute node.

Get attribute value - getAttribute()

The getAttribute() method returns the value of the attribute.

The following code retrieves the text value of the 'lang' attribute of the first <title> element:

xmlDoc=loadXMLDoc("books.xml");
txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");

Result: txt = "en"

Example explanation:

  • By using loadXMLDoc() Set "books.xmlLoad into xmlDoc
  • Set the value of the txt variable to the "lang" attribute of the first title element node

TIY

Traverse all <book> elements and get their "category" attributes:TIY

Get attribute value - getAttributeNode()

The getAttributeNode() method returns the attribute node.

The following code retrieves the text value of the "lang" attribute of the first <title> element:

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].getAttributeNode("lang");
txt=x.nodeValue;

Result: txt = "en"

Example explanation:

  • By using loadXMLDoc() Set "books.xmlLoad into xmlDoc
  • Get the "lang" attribute node of the first <title> element node
  • Set the value of the attribute to the txt variable

TIY

Loop through all <book> elements and get their "category" attributes:TIY