0

I am trying to run the sample code on W3School http://www.w3schools.com/xml/xml_to_html.asp

I copied the html+js codes and saved into a html file. I also downloaded their cd_catalog.xml and put it right beside the html file.

But when I run the html on Chrome, it shows nothing. It is also not working on IE.

Seems the problem is on the line "xmlhttp.send()", because I tried to put an alert right before it, the alert showed, but if I put the alert after that line, then it won't show up.

Anyone can help please?

<html>
<body>

<script type="text/javascript">
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","cd.xml",false);
alert("before");
xmlhttp.send();
alert("after");
xmlDoc=xmlhttp.responseXML; 

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
  { 
  document.write("<tr><td>");
  document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
  document.write("</td><td>");
  document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
  document.write("</td></tr>");
  }
document.write("</table>");
</script>

</body>
</html>

the xml file is on http://www.w3schools.com/xml/cd_catalog.xml

2
  • copy and paste the edited file too. That could help. Commented Oct 5, 2011 at 4:59
  • Check javascript console output in Chrome and/or IE for error description. Commented Oct 5, 2011 at 6:11

2 Answers 2

1

This will not work if you are running the page as a local file. You need to run it through a webserver (like Apache) in order for the http client/server conversation to work. Can you verify that you are running it through a web server?

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

3 Comments

hi @arronfrost - could you tell me, is placing the code on a free hosting site equivalent to running the code on a local web server?
Yes. As long as the file is being served from the http/https protocol, then it is the same.
thanks @arronfrost - sorry about the intermittent replies. I am not having any luck with my code on there, is it OK to place all the files in public html dir? Perhaps I should open a question...
0

@user956159: Please give your extract file location/path in the xmlhttp.open.

xmlhttp.open("GET","/Users/karthik/Desktop/cd_catalog.xml",false); 

This is in Mac file path location, I've tested it in my system & it works fine after changing the path of the file.

you can use "File://users/karthikin/mydocuments/cd_catalog.xml" or "File://C:/cd_catalog.xml" If you are using windows try this in the xmlhttp.open.

The above method to load the files locally, if you want to run through a webserver (like Apache) your code will work exactly.

Comments

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.