Jump to content

XML not working - can't see what's wrong


Gilbert

Recommended Posts

I copied and pasted a file from the w3 tutorial XML/AJAX application but changed just a couple of spots so it could read my own XML data file.  It doesn't work - what am I missing, please.  When I open the console in either google or IE it says xmlDoc is null or undefined - somehow I'm not 'reading' the file or something?  can anybody explain it more.  I've read over the toturials a couple of times and I think it should be working.  I have also cleaned out the cach in the browser by hitting cntrl F5, which I learned from someone along the way.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>S.C. Information</title>
    <style>
    table, th, td {
       border: 1px solid green;
       border-collapse:collapse;
    }
    th, td {
      padding: 5px;
    }
    </style>
</head>
 
<body>
 
    <button type="button" onclick="loadXMLDoc()">Get my S.C. Report</button>
    <br><br>
    <table id="demo"></table>
 
    <script>
        function loadXMLDoc() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
              myFunction(this);
            }
            };
            xmlhttp.open("GET", "scTotals.xml", true);
            xmlhttp.send();
        }
        function myFunction(xml) {
          var i;
          var xmlDoc = xml.responseXML;
          var table="<tr><th>Service Club</th><th>Seasonal Total</th></tr>";
          var x = xmlDoc.getElementsByTagName("Totals");
          for (i = 0; i <x.length; i++) {
            table += "<tr><td>" +
            x.getElementsByTagName("ServClub")[0].childNodes[0].nodeValue +
            "</td><td>" +
            x.getElementsByTagName("TotalAmount")[0].childNodes[0].nodeValue +
            "</td></tr>";
           }
          document.getElementById("demo").innerHTML = table;
        }
    </script>
 
</body>
</html>
 
Data File 'scTotals.xml' in same website folder.
 
<?xml version='1.0' encoding='utf-8'?>
<dataroot>
    <Totals>
        <ServClub>Beukendall Eastern Star</ServClub>
        <TotalAmount>$ 447</TotalAmount>
    </Totals>
    <Totals>
        <ServClub>Key Club</ServClub>
        <TotalAmount>$ 363</TotalAmount>
    </Totals>
    <Totals>
        <ServClub>S.I. Group</ServClub>
        <TotalAmount>$ 201</TotalAmount>
    </Totals>
    <Totals>
        <ServClub>Sarah Bilofsky & Friends</ServClub>
        <TotalAmount>$ 182</TotalAmount>
    </Totals>
    <Totals>
        <ServClub>Schenectady ARC</ServClub>
        <TotalAmount>$ 1530</TotalAmount>
    </Totals>   
</dataroot>
 
Any help would be appreciated.   Gil
 
Link to comment
Share on other sites

Thank you so much for your reply.  Sorry it took so long to get back to you - it seems I don't get an email when someone replies.   Both of your answers worked out perfectly and I can now do the XML with no problems.  Just for future reference, I don't know why the file I copied came out like triple spaced.  I copied and pasted - is there a better way?  And I don't see where to mark this question as answered.  In other forums they usually have a button right here that says 'answered'.   Thanx again dsonesuk!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...