0

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>
4
  • I made this JSFiddle jsfiddle.net/AwaaR with your code, some minor mods to use the echo/xml (made it post and pass in xml to echo) and it works fine, so as Zoli says in his answer looks like your problem lies elsewhere, I also agree with with Zoli that you should look at JSON and JQuery/Mootools or others JS frameworks. Commented Feb 26, 2012 at 22:48
  • Thanks for the help, I mention in the comment to Zoli that this code I've posted isn't what I'm working on, but an example I found at w3schools. I copied their example to see if the problem was with my code or some local host setting. Apparently, it's some local host setting but I don't know where to start on that. I didn't think I'd need some kind of serverside process to pull data from an XML file. Oh well... Commented Feb 26, 2012 at 23:02
  • Yeah, it's something local. I used the code you built and tried it on my local machine and still got nothing. Any ideas on browser settings? Commented Feb 27, 2012 at 14:35
  • Yeah, it's something local. I used the code you built and tried it on my local machine and still got nothing. Any ideas on browser settings? Here's the actual code I'm working with. jsfiddle.net/9BZCp/3 I took out the call to the XML data before I pulled my hair out. I'm going to try putting it back in and seeing if it works in jsfiddle. Commented Feb 27, 2012 at 16:40

1 Answer 1

1
  1. I think you have some problem with you url. I'm not sure that 'cd_catalog.xml' is it. Maybe use full absulute url.

  2. I recommend to you use jQuery POST with JSON instead of this code.

Have you some server side code?

Sign up to request clarification or add additional context in comments.

2 Comments

I copied the XML file from the site where I got the example. I'm not using this code, the code I have generated is different, but while trying to deduce exactly where the problem was, I broke it down to the simplest scenario, which is the call to the XML data. This is just an example that I found online that knew worked and tried to do it on my local computer. When it didn't work on my computer, I knew my problem wasn't with my code, but with some setting. Do you need a serverside process to post data from an XML file?
No, but I think if your data came from the server side as a JSON object you can use jQuery end wrote a simple code. Your example is too heavy, really old style code. If you would like to continue in that way try to debug with "debugger;" javascript keyword.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.