Jump to content
Gilbert

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

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
 

Share this post


Link to post
Share on other sites

'&'  on its own is not allowed in xml, use &amp; instead, also to ref each item through loop  you have to use

x[i]

 as well

Edited by dsonesuk

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...