Hello folks! welcome back to a new edition of our tutorial on XML DOM. In this tutorial guide, we are going to be discussing about how to access XML DOM Nodes which are considered as the informational units of the XML document.
The node structure of the XML DOM allows the developer to navigate around the tree in search for a specific information and then simultaneously access the information.
The node structure of the XML DOM allows the developer to navigate around the tree in search for a specific information and then simultaneously access the information.
Accessing Nodes
The following below are the three ways in which you can access the nodes -
- By using the getElementByTagName() method.
- By looping through or traversing through nodes tree.
- By navigating the node tree, using the node relationships
READ: XML DOM | Node Tree
getElementsByTagName() Method
This method allows access you to access the information of a node by specifying the node name. It also allows you to access the information of the Node List and Node List Length.
Syntax
The following below is the syntax to use this syntax -
node.getElementByTagName("tagname");
Where,
- node is the document node.
- tagname holds the name of the node whose value you intend getting.
Example
The following example below is a simple program which demonstrates the usage of the getElementByTagName() method -
<!DOCTYPE html> <html> <body> <div> <b>FirstName:</b> <span id = "FirstName"></span><br> <b>LastName:</b> <span id = "LastName"></span><br> <b>Category:</b> <span id = "Employee"></span><br> </div> <script> if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","/dom/node.xml",false); xmlhttp.send(); xmlDoc = xmlhttp.responseXML; document.getElementById("FirstName").innerHTML = xmlDoc.getElementsByTagName("FirstName")[0].childNodes[0].nodeValue; document.getElementById("LastName").innerHTML = xmlDoc.getElementsByTagName("LastName")[0].childNodes[0].nodeValue; document.getElementById("Employee").innerHTML = xmlDoc.getElementsByTagName("Employee")[0].attributes[0].nodeValue; </script> </body> </html>
READ: XML DOM | Model
- In the above example, we are accessing the information of the nodes FirstName, LastName and Employee.
- xmlDoc.getElementByTagName("FirstName")[0].childNode[0].nodeValue; This lines accesses the value for the child node FirstName using the getElementByTagName() method.
- xmlDoc.getElementByTagName("Employee")[0].attributes[0].nodeValue; This lines accesses the attribute value of the node Employee using the getElementByTagName() method.
Traversing Through Nodes
This is covered in our tutorial on XML DOM Traversing with few examples.
Navigating Through Nodes
This is covered in our tutorial on XML DOM Navigation with few examples.
READ: XML DOM | Methods
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 Get 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.
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.