0

I am trying to import data from an XML file. The code I am using is saved as an HTML file in the same directory as the XML file. However, the code fails to read the xml file. I am not certain I have defined the path to the file correctly. My code is as follows;

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>XML Data Download</title>

</head>

<body>
<script>
alert("Help");

    if (window.XMLHttpRequest) {
        // code for modern browsers
        xmlhttp = new XMLHttpRequest();
     } else {
        // code for old IE browsers
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 

    xhttp.open("GET", "Players.xml", false);
    alert("help2");
    xhttp.send();
    var xmlDoc = xmlhttp.responseXML;

    document.write("<table border='1'>");
    var x = xmlDoc.getElementsByTagName("Player");
    for (i - 0; i< x.length; i++) {
        alert(x.length);
        document.write("<tr><td>");
        document.write(x[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("TeleNo")[0].childNodes[0].nodeValue);
        document.write("</td></tr>");
    }
    document.write("</table>");

</script>
</body>
</html>

I have used 2 alert messages to show where the code fails. The first alert message works but the second [alert("help2");] message does not show. This is following [ xhttp.open("GET", "Players.xml", false);] Ant ideas why the file is not being read? The XML file work okay in a VB.Net application I have written.

  • 1
    watch out, when you call `document.write()` that's replacing the entire page. See https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – Daniel Manta Jan 27 '18 at 23:49
  • Thank you for the advice. I am using this code as a demo to test I can load the xml file. I will then use the loaded data to populate the options in a number of select dropdown lists, in my proper html code. – DaveTheGolfer Jan 28 '18 at 11:09

1 Answers1

0

I stupidly forgot to post the files to my server. Tried to run it directly from my desktop. Transferred files from Dreamweaver to Eclipse and then invoked Apache Tomcat. XML file now read and acted upon.