使用PHP生成XML文件,但不保存它

时间:2013-11-15 03:06:54

标签: php xml

我需要使用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();
}

1 个答案:

答案 0 :(得分:1)

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

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

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