1

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();
}
3
  • Json would be more appropriate for this can we see your ajax call ? Commented Nov 15, 2013 at 3:14
  • @TomToms You don't know that at all. The OP may be working with a client application that specifically requires an XML response Commented Nov 15, 2013 at 3:21
  • FYI Looks like your x lookup should be for <msg> elements, not <message> Commented Nov 15, 2013 at 3:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Seems to have fixed the issue thanks, forgot about that. What's the best way to access this information now? I added my JavaScript, which I know is wrong.
@MartynLeeBall Looks like you're doing it (mostly) right. I'd just use jQuery and JSON responses though
I don't like using jQuery, i'd prefer how to learn how to do it myself. But thanks
@MartynLeeBall Suit yourself. There's "learning how to do it yourself" and then there's "re-inventing the wheel". In any case, JSON is much easier to work with in JavaScript than XML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.