Jump to content

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>
    <meta charset="utf-8">
    <title>S.C. Information</title>
    table, th, td {
       border: 1px solid green;
    th, td {
      padding: 5px;
    <button type="button" onclick="loadXMLDoc()">Get my S.C. Report</button>
    <table id="demo"></table>
        function loadXMLDoc() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
            xmlhttp.open("GET", "scTotals.xml", true);
        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 +
          document.getElementById("demo").innerHTML = table;
Data File 'scTotals.xml' in same website folder.
<?xml version='1.0' encoding='utf-8'?>
        <ServClub>Beukendall Eastern Star</ServClub>
        <TotalAmount>$ 447</TotalAmount>
        <ServClub>Key Club</ServClub>
        <TotalAmount>$ 363</TotalAmount>
        <ServClub>S.I. Group</ServClub>
        <TotalAmount>$ 201</TotalAmount>
        <ServClub>Sarah Bilofsky & Friends</ServClub>
        <TotalAmount>$ 182</TotalAmount>
        <ServClub>Schenectady ARC</ServClub>
        <TotalAmount>$ 1530</TotalAmount>
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


 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

When inserting coding select '<>‘ above.

There has never been a marked as solved option, people sometimes add '[solved]' to original title.

Share this post

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