<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
var txt,x,xx,i;
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    txt="<table border='1'><tr><th>Title</th><th>Artist</th><th>COUNTRY</th><th>COMPANY</th><th>PRICE</th><th>YEAR</th></tr>";
    x=xmlhttp.responseXML.documentElement.getElementsByTagName("CD");
    for (i=0;i<x.length;i++)
      {
      txt=txt + "<tr>";
      xx=x[i].getElementsByTagName("TITLE");
        {
        try
          {
          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
          }
        catch (er)
          {
          txt=txt + "<td> </td>";
          }
        }
      xx=x[i].getElementsByTagName("ARTIST");
        {
        try
          {
          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
          }
        catch (er)
          {
          txt=txt + "<td> </td>";
          }
        }
xx=x[i].getElementsByTagName("COUNTRY");
        {
        try
          {
          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
          }
        catch (er)
          {
          txt=txt + "<td> </td>";
          }
        }
xx=x[i].getElementsByTagName("COMPANY");
        {
        try
          {
          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
          }
        catch (er)
          {
          txt=txt + "<td> </td>";
          }
        }
xx=x[i].getElementsByTagName("PRICE");
        {
        try
          {
          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
          }
        catch (er)
          {
          txt=txt + "<td> </td>";
          }
        }
xx=x[i].getElementsByTagName("YEAR");
        {
        try
          {
          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
          }
        catch (er)
          {
          txt=txt + "<td> </td>";
          }
        }
      txt=txt + "</tr>";
      }
    txt=txt + "</table>";
    document.getElementById('txtCDInfo').innerHTML=txt;
    }
  }
xmlhttp.open("GET",cd_catalog.xml,true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="txtCDInfo">
<button onClick="loadXMLDoc('cd_catalog.xml')">Get CD info</button>
</div>

</body>
</html>

Dani AI

Generated

The example in the first post is nearly there, but a few small issues usually stop it from working. The button is calling the loader with a filename while the function as written doesn’t accept that argument, and the request URL must be passed as a string when you call open. Also, running the page from the file system (file://) commonly triggers same-origin restrictions — check the browser console for CORS or network errors.

Quick troubleshooting checklist:

  • Open Developer Tools → Network and Console. Confirm the XML request returns 200 and that the response body looks like valid XML.
  • If responseXML is null, inspect responseText to see parsing errors; XML must be well-formed and tag names are case-sensitive.
  • Ensure the server sends a proper XML content-type (text/xml or application/xml).
  • Prefer textContent over firstChild.nodeValue when reading element text nodes; it’s simpler and safer.

A modern, robust approach is to fetch the file and parse it with DOMParser. This avoids legacy ActiveX fallbacks and gives clearer error handling:

fetch('cd_catalog.xml')
  .then(r => { if (!r.ok) throw new Error(r.status); return r.text(); })
  .then(txt => new DOMParser().parseFromString(txt, 'application/xml'))
  .then(doc => {
    const cds = doc.getElementsByTagName('CD');
    // iterate cds and read element.textContent
  })
  .catch(err => console.error('XML load/parse error:', err));

For local testing, run a simple HTTP server (for example python -m http.server or an npm http-server) so the browser treats requests as HTTP. See MDN for details on same-origin restrictions and parsing helpers: Same-origin policy · XMLHttpRequest.responseXML · DOMParser / Fetch API. This should help pinpoint the exact failure and get the XML displayed.

hi..i dont know y this is not working..please can anyone help me out with this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.