Reputation: 6698
I've been pulling my hair out with trying to populate a form element by pulling info out of an XML doc. I have scoured every resource I know, googled the heck out of it, even asked a few questions here, but to no avail.
I thought the problem was with me and my syntax, not knowing my backside from a hole in the ground. So, to eliminate my self from the equation, I straight copied examples from the web (which worked when tested on the source site) and put them on my local machine to try it out, and wouldn't you know, they don't work! So it's not just me! Yay!
So am I missing something when trying to process an XML file? Is there some local host setting or browser setting that I'm missing?
For a reference, here's the example I tried replicating on my computer:
<html>
<head>
<script type="text/javascript">
function loadXMLDoc(url)
{
var xmlhttp;
var txt,x,xx,i;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
txt="<table border='1'><tr><th>Title</th><th>Artist</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>";
}
}
txt=txt + "</tr>";
}
txt=txt + "</table>";
document.getElementById('txtCDInfo').innerHTML=txt;
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="txtCDInfo">
<button onclick="loadXMLDoc('cd_catalog.xml')">Get CD info</button>
</div>
</body>
</html>
Upvotes: 0
Views: 120
Reputation: 250
I think you have some problem with you url. I'm not sure that 'cd_catalog.xml' is it. Maybe use full absulute url.
I recommend to you use jQuery POST with JSON instead of this code.
Have you some server side code?
Upvotes: 1