XML DOM - Get Node
XML DOM - Get Node
In this chapter, we will study about how to get the node value of a XML DOM object. XML
documents have a hierarchy of informational units called nodes. Node object has a property
nodeValue, which returns the value of the element.
In the following sections, we will discuss −
Getting node value of an element
<Company>
<FirstName>Tanmay</FirstName>
<LastName>Patil</LastName>
<ContactNo>1234567890</ContactNo>
<Email>[email protected]</Email>
</Employee>
<FirstName>Taniya</FirstName>
<LastName>Mishra</LastName>
<ContactNo>1234667898</ContactNo>
<Email>[email protected]</Email>
</Employee>
<FirstName>Tanisha</FirstName>
<LastName>Sharma</LastName>
<ContactNo>1234562350</ContactNo>
<Email>[email protected]</Email>
</Employee>
</Company>
Example
https://www.tutorialspoint.com/dom/xml_dom_get_node.htm 1/3
23/10/2021 16:33 XML DOM - Get Node
<!DOCTYPE html>
<html>
<body>
<script>
if (window.XMLHttpRequest) {
} else{
xmlhttp.open("GET","/dom/node.xml",false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
x = xmlDoc.getElementsByTagName('FirstName')[0]
y = x.childNodes[0];
document.write(y.nodeValue);
</script>
</body>
</html>
Execution
Save this file as getnode_example.htm on the server path (this file and node.xml should be on
the same path in your server). In the output, we get the node value as Tanmay.
Attributes are part of the XML node elements. A node element can have multiple unique
attributes. Attribute gives more information about XML node elements. To be more precise, they
define properties of the node elements. An XML attribute is always a name-value pair. This
value of the attribute is called the attribute node.
The getAttribute() method retrieves an attribute value by element name.
Example
<!DOCTYPE html>
<html>
<body>
<script>
if (window.XMLHttpRequest) {
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
https://www.tutorialspoint.com/dom/xml_dom_get_node.htm 2/3
23/10/2021 16:33 XML DOM - Get Node
xmlhttp.open("GET","/dom/node.xml",false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
x = xmlDoc.getElementsByTagName('Employee')[2];
document.write(x.getAttribute('category'));
</script>
</body>
</html>
Execution
Save this file as get_attribute_example.htm on the server path (this file and node.xml should be
on the same path in your server). In the output, we get the attribute value as Management.
https://www.tutorialspoint.com/dom/xml_dom_get_node.htm 3/3