topical media & game development

talk show tell print

professional-javascript-15-MozillaXmlDomExample3.htm / htm



  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  
  <html>
      <head>
          <title>Mozilla XML DOM Example</title>
          <script type="text/javascript">
          Document.prototype.loadXML = function (sXml) {
      
              var oParser = new DOMParser();
              var oXmlDom = oParser.parseFromString(sXml, "text/xml");
           
              while (this.firstChild) {
                  this.removeChild(this.firstChild);
              }
          
              for (var i=0; i < oXmlDom.childNodes.length; i++) {
                  var oNewNode = this.importNode(oXmlDom.childNodes[i], true);
                  this.appendChild(oNewNode);
              }
          
          };
  
          </script>
      </head>
      <body>
          <p>This example loads an XML file using <code>loadXML()</code>.</p>
          <script type="text/javascript">
              var oXmlDom = document.implementation.createDocument("","",null);
  
              oXmlDom.loadXML("<root><child/></root>");
              
              alert("Tag name of the root element is " + oXmlDom.documentElement.tagName);
              alert("The root element has this many children: " + oXmlDom.documentElement.childNodes.length);
              
          </script>
   
   
      </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.