0

I'm trying to insert some xml-data into a webpage, followed several tutorials, searched the internet for examples wich I can adapt into my situation but unfortunately I didn't managed to get one running. I've even copy/pasted the example on the W3schools website to test it and adapt it later into the website I'm making. But the result stays a empty page...

What I have so far...

Body-tag:

<body>
    <table id="demo"></table>
</body>

Javascript:

    <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","index.xml",false);
xmlhttp.send();
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>

XML-file:

<CATALOG>
    <CD>
        <TITLE>Empire Burlesque</TITLE>
        <ARTIST>Bob Dylan</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>Columbia</COMPANY>
        <PRICE>10.90</PRICE>
        <YEAR>1985</YEAR>
    </CD>
    <CD>
        <TITLE>Hide your heart</TITLE>
        <ARTIST>Bonnie Tyler</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>CBS Records</COMPANY>
        <PRICE>9.90</PRICE>
        <YEAR>1988</YEAR>
    </CD>
</CATALOG>
  • Open you browser's Developer's Tools. Look in the Console. Look to see what errors you are getting. I expect that you have a security error because you are trying to read stuff off your hard disk instead of using a web server. – Quentin Nov 01 '15 at 12:58
  • XMLHttpRequest cannot load file:///C:/Users/Michael/Documents/My%20Websites/Karen/xmldata/index.xml. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.(anonymous function) @ index.html:128 index.html:128Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///C:/Users/Michael/Documents/My%20Websites/Karen/xmldata/index.xml' – Michael Tytgat Nov 01 '15 at 13:02
  • Indeed! When I upload it, it works... How can I enable my browser to view it offline to test things easily? – Michael Tytgat Nov 01 '15 at 13:06
  • Run a local web server if you want to do web development locally. – Quentin Nov 01 '15 at 14:06

0 Answers0