1

我需要用 PHP 生成一个 XML 文件,然后将其作为 XML 响应返回给 AJAX 请求。但我注意到如果不使用实际的 xml 文件,它就无法做到这一点。

无论如何强迫它认为它实际上是XML,我不能每次都保存生成的XML,因为它每10秒刷新一次并再次编译它。

这是生成 XML 的 PHP

<?php
session_start();
include "../includes/db_connect.php";
include "../includes/required.php";

//Get the information from the database
$query = "SELECT message, id, date, messerror FROM messages WHERE dismiss = 0 LIMIT ?";
if ($stmt = $mysqli->prepare($query)) {
    $stmt->bind_param("i", $settings['common']['max_errors']);
    $stmt->execute();

    //Bind variables to prepared statement
    $stmt->bind_result($message,$id,$date,$messerror);

    $doc = new DOMDocument('1.0');
    $doc->formatOutput = true;
    $root = $doc->createElement('messages');
    $root = $doc->appendChild($root);

    //Fetch values
    while ($stmt->fetch()) {
        //Create root node
        $block = $doc->createElement('msg');
        $block = $root->appendChild($block);

        //Create sub nodes for ID
        $id_node = $doc->createElement('id');
        $id_node = $block->appendChild($id_node);
        $text = $doc->createTextNode($id);
        $text = $id_node->appendChild($text);

        //Create sub nodes for date
        $date_node = $doc->createElement('date');
        $date_node = $block->appendChild($date_node);
        $text = $doc->createTextNode($date);
        $text = $date_node->appendChild($text);

        //Create sub nodes for message
        $message_node = $doc->createElement('message');
        $message_node = $block->appendChild($message_node);
        $text = $doc->createTextNode($message);
        $text = $message_node->appendChild($text);

        //Create sub nodes for message or error
        $messerror_node = $doc->createElement('messerror');
        $messerror_node = $block->appendChild($messerror_node);
        $text = $doc->createTextNode($messerror);
        $text = $messerror_node->appendChild($text);
    }
    echo $doc->saveXML()."\n";

    //Close statement
    $stmt->close();
}
$mysqli->close();
?>

这是生成的 XML

<?xml version="1.0"?>
<messages>
  <msg>
    <id>hello</id>
    <date>14/11/2013 20:37</date>
    <message>Successfully logged in!</message>
    <messerror>message</messerror>
  </msg>
  <msg>
    <id>hello</id>
    <date>15/11/2013 00:52</date>
    <message>Successfully logged in!</message>
    <messerror>message</messerror>
  </msg>
  <msg>
    <id>hello</id>
    <date>15/11/2013 02:42</date>
    <message>Successfully logged in!</message>
    <messerror>message</messerror>
  </msg>
</messages>

JavaScript 调用,它可能是错误的,但我知道它没有将响应解释为 XML:

function error_checking() {
    var http = getHTTPObject();
    http.onreadystatechange=function() {
        if (http.readyState==4 && http.status==200) {
            var x = http.responseXML.getElementsByTagName("messages");
            var xx = x[0].getElementsByTagName("msg");
            doc("test").innerHTML = xx[0].firstChild.nodeValue;
        }
    }
    http.open("GET","php/check_errors.php",true);
    http.send();
}
4

1 回答 1

1

我想我看到了问题。您需要发送适当的内容类型标头,例如

header('Content-type: text/xml');
echo $doc->saveXML();

我还会删除结束 PHP 标记 ( ?>),因为您不需要它,这可能会导致麻烦。

于 2013-11-15T03:12:06.040 回答