Why does the following Java Script fail to load XML?

Posted by Pavitar on Stack Overflow See other posts from Stack Overflow or by Pavitar
Published on 2010-12-26T06:17:11Z Indexed on 2010/12/26 6:54 UTC
Read the original article Hit count: 279

Filed under:
|
|
|

I have taken an example taught to us in class,wherein a javascript is used to retrieve data from the XML,but it doesn't work.Please help I have also added the XML file below.

<html>
    <head>
             <title>Customer Info</title>
    <script language="javascript">
      var xmlDoc = 0;
      var xmlObj = 0;

      function loadCustomers(){
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");  
        xmlDoc.async = "false";
        xmlDoc.onreadystatechange = displayCustomers;
        xmlDoc.load("customers.xml");
      }

      function displayCustomers(){
        if(xmlDoc.readyState == 4){
        xmlObj = xmlDoc.documentElement;
        var len = xmlObj.childNodes.length;         
        for(i = 0; i < len; i++){
          var nodeElement = xmlObj.childNodes[i];
          document.write(nodeElement.attributes[0].value);
          for(j = 0; j < nodeElement.childNodes.length; j++){
            document.write(" " + nodeElement.childNodes[j].firstChild.nodeValue);
          }
          document.write("<br/>");
        }
        }
          }
        </script>
    </head>
    <body>
        <form>
            <input type="button" value="Load XML" onClick="loadCustomers()">
        </form>
    </body>
</html>

XML(customers.xml)

<?xml version="1.0" encoding="UTF-8"?>

<customers>
    <customer custid="CU101">
        <pwd>PW101</pwd>
        <email>[email protected]</email>
    </customer>
    <customer custid="CU102">
        <pwd>PW102</pwd>
        <email>[email protected]</email>
    </customer>
    <customer custid="CU103">
        <pwd>PW103</pwd>
        <email>[email protected]</email>
    </customer>
    <customer custid="CU104">
        <pwd>PW104</pwd>
        <email>[email protected]</email>
    </customer>
</customers>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html