简体   繁体   中英

Unable to get .xml file to load in to .php file using html + javascript

I don't need anything fancy on this. I'm loading a Q&A page. The Q&A I want on a seperate file so I can just update the file and it will update the page the next time it's loaded by someone.

I created a file "faq.xml" and am trying to load that in to my faq.php file.

<p id="xmlp" class="content" style="text-align: center">
    <!-- XML should go here -->
</p>
<script>
    function loadXMLDoc() {
        var xmlhttp = new XMLHttpRequest();
        myFunction(this);

        xmlhttp.open("GET", "faq.xml", true);
        xmlhttp.send();
    }

    function myFunction(xml) {
        var x, i, xmlDoc, table;
        xmlDoc = xml.responseXML;
        table = "<tr><th>Question</th><th>Answer</th></tr>";
        x = xmlDoc.getElementsByTagName("faqs")
        for (i = 0; i < x.length; i++) {
            table += "<tr><td>" +
                    x[i].getElementsByTagName("question")[0].childNodes[0].nodeValue +
                    "</td><td>" +
                    x[i].getElementsByTagName("answer")[0].childNodes[0].nodeValue +
                    "</td></tr>";
        }
        document.getElementById("xmlp").innerHTML = table;
    }
</script>

For whatever reason, it's not loading anything. The XML file currently has this...

<?xml version="1.0" encoding="UTF-8"?>
<faqs>
    <question>When do you fly?</question>
    <answer>We fly at sunrise weather permitting.  Flights are more peaceful and the winds are great.  We even fly low enough to have a conversation with those less fortunate on the ground.  People tend to hang around at launch and landings to see what it’s all about.</answer>
</faqs>

Any suggestions? I'm new to XML (this is my first one).

Thank you in advanced.


I put an alert in to ensure the js was getting started and it did function.

<script>
    loadXMLDoc();
    function loadXMLDoc() {
        var xmlhttp = new XMLHttpRequest();
        alert();
        myFunction(this);

        xmlhttp.open("GET", "faq.xml", true);
        xmlhttp.send();
    }

Still not sure where the hold up is.


I did more testing and I think I found where at least something is wrong...

<p id="xmlp" class="content" style="text-align: center">
    <!-- XML should go here -->
</p>
<script>
    function loadXMLDoc() {
        var xmlhttp = new XMLHttpRequest();
        myFunction(this);

        xmlhttp.open("GET", "faq.xml", true);
        xmlhttp.send();
    }

    function myFunction(xml) {
        var x, i, xmlDoc, table;
        xmlDoc = xml.responseXML;
        table = "<tr><th>Question</th><th>Answer</th></tr>";
alert("This one works");
        x = xmlDoc.getElementsByTagName("faqs")
alert("This one does not work");
        for (i = 0; i < x.length; i++) {
            table += "<tr><td>" +
                    x[i].getElementsByTagName("question")[0].childNodes[0].nodeValue +
                    "</td><td>" +
                    x[i].getElementsByTagName("answer")[0].childNodes[0].nodeValue +
                    "</td></tr>";
        }
        document.getElementById("xmlp").innerHTML = table;
    }
</script>

This is a basic example of loading an xml file via AJAX from https://www.w3schools.com/js/tryit.asp?filename=tryjs_ajax_xml :

<body onLoad="loadXMLDocument('someXMLfile.xml')">

<p>
   <b>Status:</b> <span id="statusInfo"></span>
</p>
<p>
   <b>Status text:</b><span id="statusTextInfo"></span>
</p>
<p>
   <b>Response:</b> <span id="xmlContent"></span>
</p>



<script>
function loadXMLDocument(url) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById('statusText').innerHTML = this.status;
      document.getElementById('statusTextInfo').innerHTML = this.statusText;
      document.getElementById('xmlContent').innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", url, true);
  xhttp.send();
}
</script>

</body>

This will load an XML file into Javascript. Though as I mentioned, if you're getting to grips with AJAX, PHP and XML all at once the straightforward way might be to make an AJAX call to PHP and process the xml using a php function on the server.

The 4/200 status codes make AJAX debugging a little easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM