如何解决xml声明问题

时间:2016-12-02 19:48:32

标签: javascript php html ajax xml

我收到错误"此页面包含以下错误: 第13行第10行的错误:仅在文档开头允许的XML声明下面是第一个错误的页面呈现"。

我试图通过使用require()在php文件中打开html文件,我也使用javascript文件来运行程序。我想在表格中显示我的php数据库中的数据。

也许我应该在同一个文件中使用javascript和php?非常感谢任何有助于此工作的帮助。谢谢。

airportArrivals.html

<html>
<head>  
<script type="text/javascript"> </script>
</head>
<hr></hr>

<hr></hr>
<div id="myDiv"></div>
<a href="http://localhost/airport/airportArrivals.php">Arrivals</a> | <a href="http://localhost/airport/liveArrivalsJS.php">LiveArrivals(JS)</a> | <a href="http://localhost/airport/liveArrivalsNode.js">Live Arrivals(Node)</a> | <a href="http://localhost/airport/admin.php">Admin</a>
</html>

liveArrivalsJS.js

window.onload=function(){
    getAjaxData();
}

var xmlhttp;

function getAjaxData()
{   
    if (window.XMLHttpRequest)
    {
        // IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        // IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }   
    xmlhttp.onreadystatechange = showXMLData;       
    xmlhttp.open("GET", "http://localhost/airport/liveArrivalsJS.php", true);       
    xmlhttp.send(); 
}

function showXMLData()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)   {       

        var terminal = xmlhttp.responseXML.getElementsByTagName("terminal");
        var origin = xmlhttp.responseXML.getElementsByTagName("origin");
        var airline = xmlhttp.responseXML.getElementsByTagName("airline");
        var flight = xmlhttp.responseXML.getElementsByTagName("flight");
        var scheduledDate = xmlhttp.responseXML.getElementsByTagName("scheduledDate");
        var scheduledTime = xmlhttp.responseXML.getElementsByTagName("scheduledTime");
        var status = xmlhttp.responseXML.getElementsByTagName("status");            

        var output = '<table border = 1>';                     
        for (var i=0;i<terminal.length;i++) {
            output += '<tr><td>' + terminal[i].firstChild.nodeValue + '</td><td>' + origin[i].firstChild.nodeValue + '</td><td>' + airline[i].firstChild.nodeValue +'</td></tr>' + flight[i].firstChild.nodeValue + '</td><td>'+ scheduledDate[i].firstChild.nodeValue + '</td><td>'+ scheduledTime[i].firstChild.nodeValue + '</td><td>'+ status[i].firstChild.nodeValue + '</td><td>'
        }
        output += '</table>';
        document.getElementById("myDiv").innerHTML = output;            
    }
}

liveArrivalsJS.php

<?php
require 'airportArrivals.html';

$xmlDom = new DOMDocument();
$xmlDom->appendChild($xmlDom->createElement('results'));
$xmlRoot = $xmlDom->documentElement;

$connection = mysqli_connect("localhost","root","");
mysqli_select_db($connection,"airportdb");
$result = mysqli_query($connection,"select * from arrivals");

while($row=mysqli_fetch_array($result))
{
    $xmlRowElementNode = $xmlDom->createElement('row');
    $i=0;
    for($i=0;$i<mysqli_num_fields($result);$i++)
    {
        $xmlRowElement = $xmlDom->createElement($result->fetch_field_direct($i)->name);
        $xmlText = $xmlDom->createTextNode($row[$i]);
        $xmlRowElement->appendChild($xmlText);
        $xmlRowElementNode->appendChild($xmlRowElement);
    }
    $xmlRoot->appendChild($xmlRowElementNode);
}    
mysqli_close($connection);
header('Content-type:  text/xml');
print($xmlDom->saveXML());
?>

0 个答案:

没有答案