0

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>

EDIT 1

I'm trying some examples from W3Schools.com but they aren't working.I'm using chrome and also IE. here are some examples that I tried. Example1 Example2

1 Answer 1

2

It looks like the example will only work in Internet Explorer web browser . You might want to take a look a this Getting Started tutorial.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.