How to use DOM Access Nodes in XML

I this article I am going to explain about XML DOM Access Nodes.
  • 2234

Nodes Accessing in XML

Nodes Accessing is the process by which we can access  to Nodes in XML document.  With DOM we can access in every Nodes in XML document. There are three way by which we can access the Nodes.

(1) getElementsByTagName() Method

When we use getElementByTagName() Method then its return all element with tag name.

node.getElementsByTagName("tag");

Example

In following example its return all "name" element under "sub" element.

sub.getElementsByTagName("type");

In following example its return all "name" element under XML document.

Document.getElementsByTagName("name");

 

(2) Using loop through a node list

 

We use loop through a node list by the using the length properties. Length properties defined the size of length.

 

Example

 

xDoc = loadXMLDoc("Doce.xml");

 

    dd = xDoc.getElementsByTagName("name");

    for (i = 0; i < dd.length; i++) {

        document.write(dd[i].childNodes[0].nodeValue);

        document.write("<br />");

 

 

(3) Using Navigating Node Relationship

 

In following example we are using Navigating Node Relationship.

 

xDoc = loadXMLDoc("doc.xml");

 

    dd = xDoc.getElementsByTagName("name")[0].childNodes;

    mm = xDoc.getElementsByTagName("name")[0].firstChild;

    for (i = 0; i < dd.length; i++) {

        if (mm.nodeType == 3) {

            document.write(mm.nodeName + "<br />");

        }

        mm = mm.nextSibling;

    }

Further Readings

You may also want to read these related articles: here
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

 

© 2019 DotNetHeaven. All rights reserved.