HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
The nodeValue property is used to get the text value of a node.The getAttribute() method returns the value of an attribute
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.
To retrieve the text value of an element, you must retrieve the value of the elements' text node.
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 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 returns the text value of a text node.
The following code retrieves the text value of the text node of the first <title> element:
x = xmlDoc.getElementsByTagName("title")[0];
y = x.childNodes[0];
z = y.nodeValue;
Result in z: "Everyday Italian"
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
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>
Loop through all <title> elements: Try it Yourself
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.
The getAttribute() method returns an attribute's value.
The following code retrieves the text value of the "lang" attribute of the first <title> element:
x = xmlDoc.getElementsByTagName("title")[0];
txt = x.getAttribute("lang");
Result in txt: "en"
Loop through all <book> elements and get their "category" attributes:
Try
it yourself
The getAttributeNode() method returns an attribute node.
The following code retrieves the text value of the "lang" attribute of the first <title> element:
x = xmlDoc.getElementsByTagName("title")[0];
y = x.getAttributeNode("lang");
txt = y.nodeValue;
Result in txt = "en"
Loop through all <book> elements and get their "category" attributes: Try it Yourself