THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

XML DOM Get Node Values


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

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

×

Header


Get the Value of an Element

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

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

Note To retrieve the text value of an element, you must retrieve the value of the elements' text node.

The getElementsByTagName Method

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

Suppose "books.xml" has been loaded into xmlDoc.

This code retrieves the first <title> element:

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

The ChildNodes Property

The childNodes property returns a list of an element's child nodes.

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

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

The nodeValue Property

The nodeValue property returns the text value of a text node.

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

Example

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

Resul in z: "Everyday Italian"


Complete Example

Example

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        myFunction(xhttp);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();

function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName('title')[0];
    var y = x.childNodes[0];
    document.getElementById("demo").innerHTML = y.nodeValue;
}
</script>

</body>
</html>
Try it Yourself »

Loop through all <title> elements: Try it Yourself


Get the Value of an Attribute

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

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

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


Get an Attribute Value - getAttribute()

The getAttribute() method returns an attribute's value.

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

Example

x = xmlDoc.getElementsByTagName("title")[0];
txt = x.getAttribute("lang");
Try it Yourself »

Result in txt: "en"

Loop through all <book> elements and get their "category" attributes: Try it yourself


Get an Attribute Value - getAttributeNode()

The getAttributeNode() method returns an attribute node.

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

Example

x = xmlDoc.getElementsByTagName("title")[0];
y = x.getAttributeNode("lang");
txt = y.nodeValue;
Try it Yourself »

Result in txt = "en"

Loop through all <book> elements and get their "category" attributes: Try it Yourself