<!DOCTYPE html>

<html>

   <body>

      <h1>document interface </h1>     

      <script>

            function loadFile(name){

                   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",name,false);

                  xmlhttp.send();

                  return xmlhttp.responseXML;

 

            }

           

            xmlDoc = loadFile("myData.xml");

            xd = xmlDoc.documentElement;

           

            document.write("NodeName : " + xd.nodeName);

            document.write("NodeValue : " + xd.childNodes[0].nodeValue + "<br/>");

            d =xmlDoc.getElementsByTagName("data");

            for(j=0; j<d.length; j++){

                  c = d[j].childNodes;

           

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

                        if(c[i].nodeType != 3){

                              document.write("NodeName : " + c[i].nodeName);

                              document.write(" NodeType : " + c[i].nodeType);

                              document.write(" NodeValue : " + c[i].childNodes[0].nodeValue + "<br/>");

                        }

                  }

            }                

      </script>

   </body>

</html>

Output:

Document interface

NodeName : rootNodeValue : 
NodeName : animal NodeType : 1 NodeValue : Lion
NodeName : habitat NodeType : 1 NodeValue : jungle
NodeName : type NodeType : 1 NodeValue : mammal
NodeName : eatingHabit NodeType : 1 NodeValue : Carnivore
NodeName : animal NodeType : 1 NodeValue : Deer
NodeName : habitat NodeType : 1 NodeValue : jungle
NodeName : type NodeType : 1 NodeValue : mammal
NodeName : eatingHabit NodeType : 1 NodeValue : Herbivore