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

XML DOM – Getting Node Values

XML DOM Getting Node Values


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

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


Get the Value of an Element

In the DOM, every component is a node. Element nodes do not have a text value.

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

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


Get the Element's Value

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

The following code uses loadXMLDoc() Puts the books.xml to load into xmlDoc and retrieve the first element:</p> <p>xmlDoc=loadXMLDoc("books.xml");</p> <p>x=xmlDoc.getElementsByTagName("title")[0];</p> <p>The childNodes property returns a list of child nodes.<title> an element has only one child node. It is a text node.</p> <p>The following code retrieves <title> the text node of the element:</p> <p>x=xmlDoc.getElementsByTagName("title")[0];</p> <p>y=x.childNodes[0];</p> <p>The nodeValue property returns the text value of a text node:</p> <h2>Example</h2> <p>x=xmlDoc.getElementsByTagName("title")[0];</p> <p>y=x.childNodes[0];</p> <p>txt=y.nodeValue;</p> <p>Try it »</p> <p>Result: txt = "Everyday Italian"</p> <p>Traverse all <title> element: <a href="/try/try.php?filename=try_dom_getelementsbytagname1">Try it</a></p>


Get the Attribute's Value

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

The way to get an attribute's value is to get its text value.

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


Getting an Attribute Value - getAttribute()

The getAttribute() method returns the attributeValue.

The following code retrieves the first element's "lang" attribute text value:</p>

Example

xmlDoc=loadXMLDoc("books.xml");

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

Try it »

Result: txt = "en"

Example explanation:

  1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
  2. Set the txt variable to the value of the "lang" attribute of the first title element node

Iterate over all the elements, and get their "category" attribute: Try it


Getting an Attribute Value - getAttributeNode()

The getAttributeNode() method returns the attributeNode.

The following code retrieves the first element's "lang" attribute text value:</p>

Example

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title")[0].getAttributeNode("lang");

txt=x.nodeValue;

Try it »

Result: Result: txt = "en"

Example explanation:

  1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
  2. Get the first element node's "lang" attribute node</li>
  3. Set the txt variable to the attribute's value

Iterate over all the elements and get their "category" attribute: Try it