XML DOM Get Node Value
- Previous Page DOM Locate Node
- Next Page DOM Change Node
nodeValue
Attribute is used to get the text value of the node.
getAttribute()
The method returns the value of the attribute.
Get the value of the element
In the DOM, everything is a node. Element nodes do not have text values.
The text value of an element node is stored in the child node. This node is called a text node.
To get the text value of an element, you must retrieve the value of the element's text node.
That is, the method to get the element text is to get the value of this child node (text node).
getElementsByTagName method
getElementsByTagName()
The method returns a list of nodes that have the specified tag nameAll elements node list, in which the order of the elements is the order in which they appear in the source document.
Assume books.xml has been loaded into xmlDoc.
This code retrieves the first <title> element:
var x = xmlDoc.getElementsByTagName("title")[0];
ChildNodes attribute
childNodes
Attribute returnsList of child nodes of the element.
The following code retrieves the first <title> element's text node:
x = xmlDoc.getElementsByTagName("title")[0]; y = x.childNodes[0];
nodeValue attribute
nodeValue
Attribute returnsText node text value.
The following code retrieves the text value of the first <title> element's text node:
Example
x = xmlDoc.getElementsByTagName("title")[0]; y = x.childNodes[0]; z = y.nodeValue;
Result in z: On Eating in a Niche
Complete Example
Example 1
<!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>
Example 2
Traverse all <title> elements:
x = xmlDoc.getElementsByTagName('title'); for (i = 0; i < x.length; i++) { txt += x[i].childNodes[0].nodeValue + "<br>"; }
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 attribute value is to get its text value.
can be obtained by using getAttribute()
method or attribute node nodeValue
to complete this task.
Get attribute value - getAttribute()
getAttribute()
The method returnsThe value of the attribute.
The following code retrieves the attribute node of the first <title> element: "lang"
The text value of the attribute:
Example 1
x = xmlDoc.getElementsByTagName("title")[0]; txt = x.getAttribute("lang");
Example 2
Traverse all <book> elements and retrieve their "category" attribute:
x = xmlDoc.getElementsByTagName("book"); for (i = 0; i < x.length; i++) { txt += x[i].getAttribute("category") + "<br>"; }
Get attribute value - getAttributeNode()
getAttributeNode()
The method returnsattribute node.
The following code retrieves the attribute node of the first <title> element: "lang"
The text value of the attribute:
Example 1
x = xmlDoc.getElementsByTagName("title")[0]; y = x.getAttributeNode("lang"); txt = y.nodeValue;
Example 2
Traverse all <book> elements and retrieve their "category" attribute:
x = xmlDoc.getElementsByTagName("book"); for (i = 0; i < x.length; i++) { txt += x[i].getAttributeNode("category").nodeValue + "<br>"; }
- Previous Page DOM Locate Node
- Next Page DOM Change Node