topical media & game development

talk show tell print

professional-xml-12-IteratingNodes.htm / htm



  <html xmlns="http://www.w3.org/1999/xhtml">        
  <head>
    <title>Looping through an XML Document</title>
    <script type="text/javascript" language="javascript">        
          //Define a constant to represent the Element nodeType
          var ELEMENT_NODE = 1;        
          var doc;
          function btnDisplayNodeNames_Click()
          {
              loadDocument();
              displayNodeNames();            
          }
  
      function loadDocument()
      {
          doc = new ActiveXObject("Microsoft.XMLDOM");        
          doc.async = false;
          doc.load("Products.xml");    
      }        
      
          function displayNodeNames()        
          {                
          
              var productNodes = doc.getElementsByTagName("Product"); 
          var length = productNodes.length;
          for (i = 0; i < length; i++) 
              document.write(productNodes.item(i).text + "<br>");
          return;
  
              var nodes = doc.documentElement.childNodes.item(0).childNodes;
          alert(nodes.item(0).text);
          return;
  
              var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
              xmlDoc.async="false";
              xmlDoc.load("Products.xml");
              document.write("Error code: " + xmlDoc.parseError.errorCode);
              document.write("<br />Error reason: " + xmlDoc.parseError.reason);
              document.write("<br />Error line: " + xmlDoc.parseError.line);
              return;
          
              var firstChildElement = doc.documentElement.firstChild;
              var attributes = firstChildElement.attributes;            
              for (i = 0; i < attributes.length; i++) 
              document.write(attributes.item(i).name + "=" 
              + attributes.item(i).text + "<br>");
              
              return;
              
              var prodElement = doc.documentElement.firstChild;
              var text = prodElement.firstChild.firstChild;
          document.write(text.data + "<br>");        
          var lastTwoCharacters = text.substringData(1, 2)
          document.write(lastTwoCharacters  + "<br>");
  
          return;
              var firstChildElement = doc.documentElement.firstChild;
              var attributes = firstChildElement.attributes;            
              for (i = 0; i < attributes.length; i++) 
              document.write(attributes.item(i).name + "=" 
              + attributes.item(i).value + "<br>");
              
              return;
              var productNodes = doc.getElementsByTagName("Product"); 
          var length = productNodes.length;
          for (i = 0; i < length; i++) 
              document.write(productNodes.item(i).text + "<br>");
          return;
              var newElement= doc.createElement("ProductIdentifier");
              var oldElement = doc.documentElement.childNodes.item(0).childNodes.item(0);            
          doc.documentElement.childNodes.item(0).insertBefore(newElement, oldElement);                    
          alert(doc.documentElement.childNodes.item(0).xml);
          doc.documentElement.childNodes.item(0).removeChild(newElement);
          alert(doc.documentElement.childNodes.item(0).xml);
              return;
              var names = doc.getElementsByTagName("Name");
          for (var i = 0; i < names.length; i++) 
          {
            document.write(names.item(i).text + "  ");
          } 
  
              return;
              var output = "";
              //Get the number of nodes contained in the document
              var nodeCount = doc.documentElement.childNodes.length;                
              //This for loop visits each node in the childNodes collection.        
              for (index = 0; index < nodeCount; index++) 
              {        
                  var childNode = doc.documentElement.childNodes.item(index);        
                  //Check if the child is an Element node and display the tag name
                  if (childNode.nodeType == ELEMENT_NODE) 
                  {        
                      output += childNode.nodeName  + "=" + childNode.text + "\n";        
                      alert(childNode.childNodes.item(0).text);
                      //var nodes = childNode.item(0).childNodes;
                  //document.write(nodes.item(0).text);
                      
                  }        
              }        
              result.innerText = output;
          }        
    </script>        
  </head>
  <body>
      <input type="button" id="btnDisplayNodeNames" value="Display Node Names" onclick="btnDisplayNodeNames_Click()" />
      <br/><br/><br/>
      <div id="result">      
      </div>
  </body>                
  </html>        
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.