Martyn Ball
Martyn Ball

Reputation: 4889

Generating a XML file with PHP,but not saving it

I need to generate a XML file with PHP, which will then be returned to an AJAX request as an XML response. But I have noticed it can't do this without using an actual xml file.

Is there anyway to force it to think it is actually XML, I can't save the generated XML everytime as it refreshes every 10 seconds and compiles it again.

Here is the PHP generating the XML

<?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();
?>

Here is the produced 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 call, it's probably wrong, but I know it isn't interpreting the response as 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();
}

Upvotes: 1

Views: 260

Answers (1)

Phil
Phil

Reputation: 164731

I think I see the problem. You need to send the appropriate content-type header, eg

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

I'd also drop the closing PHP tag (?>) as you don't need it and it can lead to trouble.

Upvotes: 1

Related Questions