XML DOM | Set Node

XML DOM - Set Node


Hello folks! welcome back to a new edition of our tutorial on XML DOM. In this tutorial, we are going to be discussing about how to change the values of nodes in a XML DOM object.

Node values can be changed as follows -

var value = node.nodeValue;

If node is an Attribute then the value variable will be the value of the attribute; if node is a Text node, it will be the text content; if node is an Element, it will be null.


The following sections will demonstrate the node value setting for each type of node, i.e, (attribute, text node and element).

Note: We have used the node.xml in all the following examples -

<Company>
   <Employee category = "Technical">
      <FirstName>Kennedy</FirstName>
      <LastName>Nkpara</LastName>
      <ContactNo>1234567890</ContactNo>
      <Email>kennedynkpara@xyz.com</Email>
   </Employee>
   
   <Employee category = "Non-Technical">
      <FirstName>Stephanie</FirstName>
      <LastName>Francis</LastName>
      <ContactNo>1234667898</ContactNo>
      <Email>stephaniefrancis@xyz.com</Email>
   </Employee>
   
   <Employee category = "Management">
      <FirstName>Justice</FirstName>
      <LastName>Douglas</LastName>
      <ContactNo>1234562350</ContactNo>
      <Email>justicedouglas@xyz.com</Email>
   </Employee>
</Company>


Change Value of Text Node

When we say change value of Node element we means simply to edit the text content of an element (also called the text node). The following example below demonstrates how to change the text node of an element.

Example

The below example is used to parse an xml document (node.xml) into XML DOM object, and changes the value of an element's text node. In this case, Email of each Employee to support@xyz.com and prints the values -

<!DOCTYPE html>
<html>
   <head>
      <script>
         function loadXMLDoc(filename) {
            if (window.XMLHttpRequest) {
               xhttp = new XMLHttpRequest();
            } else // code for IE5 and IE6 {
                xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xhttp.open("GET",filename,false);
            xhttp.send();
            return xhttp.responseXML;
         }
      </script>
   </head>
   <body>
      <script>
         xmlDoc = loadXMLDoc("/dom/node.xml");

         x = xmlDoc.getElementsByTagName("Email");
         for(i = 0;i<x.length;i++) {	
	
            x[i].childNodes[0].nodeValue = "support@xyz.com";
            document.write(i+');
            document.write(x[i].childNodes[0].nodeValue);
            document.write('<br>');
         }
	
      </script>
   </body>
</html>

Output

Save this file as set_text_node_example.html on the server's path (this file and node.xml should be on the same path in your server). The output of the above example will be as follows -

0) support@xyz.com
1) support@xyz.com
2) support@xyz.com


Change Value of Attribute Node

The following example demonstrates how to change the attribute node of an element.

Example

The below example is used to parse an xml document (node.xml) into XML DOM object, and changes the value of an element's attributes node. In this case, the Category of each Employee to admin-0, admin-1, admin-2, respectively and print the values.

<!DOCTYPE html>
<html>
   <head>
      <script>
         function loadXMLDoc(filename) {
            if (window.XMLHttpRequest) {
                xhttp = new XMLHttpRequest();
            } else // code for IE5 and IE6 {
                xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xhttp.open("GET",filename,false);
            xhttp.send();
            return xhttp.responseXML;
         }
      </script>
   </head>
   <body>
      <script>
         xmlDoc = loadXMLDoc("/dom/node.xml");

         x = xmlDoc.getElementsByTagName("Employee");
         for(i = 0 ;i<x.length;i++){	
	
            newcategory = x[i].getAttributeNode('category');
            newcategory.nodeValue = "admin-"+i;
            document.write(i+');
            document.write(x[i].getAttributeNode('category').nodeValue);
            document.write('<br>');
         }
	
      </script>
   </body>
</html>

Output

Save file as set_node_attribute_example.html on the server's path (this file and node.xml should be on the same path in your server). The output of the above example will be as follows -

0) admin-0
1) admin-1
2) admin-2


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about XML DOM Create Node.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.