tags:

views:

35

answers:

1

Hi guys, I am creating XML using a while loop, but now I need to prepend and append the generated XML with the XML header info and wrapper tag, but I am struggling to get it to work, here is my code,

$result = mysql_query("SELECT * FROM users")
or die(mysql_error());

$row = mysql_fetch_array($result);

    while ($row = mysql_fetch_array($result)) {
        $pumaXML  = "<userDetails>";
        $pumaXML .= "<userID>".$row['uid']."</userID>";
        $pumaXML .= "<userName>".$row['userName']."</userName>";
        $pumaXML .= "<points>".$row['points']."</points>";
        $pumaXML .= "<imageURL>".$row['imageURL']."</imageURL>";
        $pumaXML .= "<thumbURL>".$row['thumbURL']."</thumbURL>";
        $pumaXML .= "</userDetails>";
    };

I can't seem to find a way to do this, I also tried making a function, but that didn't go that well, here is that code,

function createXML($result) {
    while ($row = mysql_fetch_array($result)) {
        $pumaXML  = "<userDetails>";
        $pumaXML .= "<userID>".$row['uid']."</userID>";
        $pumaXML .= "<userName>".$row['userName']."</userName>";
        $pumaXML .= "<points>".$row['points']."</points>";
        $pumaXML .= "<imageURL>".$row['imageURL']."</imageURL>";
        $pumaXML .= "<thumbURL>".$row['thumbURL']."</thumbURL>";
        $pumaXML .= "</userDetails>";
    };
    return $pumaXML;
};

Thanx in advance!

+2  A: 

Here is how to do it with DOM:

function createUserDetailsXml(array $result) {

    $dom  = new DOMDocument;
    $dom->formatOutput = TRUE; // enable automatic indenting
    $dom->loadXML('<users/>'); // set root node

    foreach($result as $row) {

        // create user-details node
        $user = $dom->createElement('user-details');

        // create and append details to user-details node
        $user->appendChild(
            $dom->createElement('user-id', $row['uid']));
        $user->appendChild(
            $dom->createElement('user-name', $row['userName']));
        $user->appendChild(
            $dom->createElement('user-points', $row['points']));
        $user->appendChild(
            $dom->createElement('image-url', $row['imageURL']));
        $user->appendChild(
            $dom->createElement('thumb-url', $row['thumbURL']));

        // add user-details node to XML document, e.g. users node
        $dom->documentElement->appendChild($user);
    };
    return $dom->saveXML(); // returns the formatted XML
};

Note that the function expects you to pass in the full result array, so I could test it with:

$result = array(
    array(
        'uid'      => 1,
        'userName' => 'Gordon',
        'points'   => PHP_INT_MAX,
        'imageURL' => 'http://example.com/gordon.jpg',
        'thumbURL' => 'http://example.com/t_gordon.jpg'
    ),
    array(
        'uid'      => 2,
        'userName' => 'John <blink>"Frigging"</blink> Doe',
        'points'   => 0,
        'imageURL' => 'http://example.com/johndoe.jpg',
        'thumbURL' => 'http://example.com/t_johndoe.jpg'
    )
);
echo createUserDetailsXml($result);

The function will then return

<?xml version="1.0"?>
<users>
  <user-details>
    <user-id>1</user-id>
    <user-name>Gordon</user-name>
    <user-points>2147483647</user-points>
    <image-url>http://example.com/gordon.jpg&lt;/image-url&gt;
    <thumb-url>http://example.com/t_gordon.jpg&lt;/thumb-url&gt;
  </user-details>
  <user-details>
    <user-id>2</user-id>
    <user-name>John &lt;blink&gt;"Frigging"&lt;/blink&gt; Doe</user-name>
    <user-points>0</user-points>
    <image-url>http://example.com/johndoe.jpg&lt;/image-url&gt;
    <thumb-url>http://example.com/t_johndoe.jpg&lt;/thumb-url&gt;
  </user-details>
</users>

Please notice that DOM escaped the special chars in John Doe's name for you automatically. DOM will also make sure the XML element names (or attributes if you use them) are syntactically valid. It also added the XML Prolog.

Gordon
+1 That's indeed the most elegant way of creating XML
DrColossos
@DrColossos thanks. The most elegant way would be to have the database return the XML directly, but I think MySql cannot do that yet.
Gordon
It is possible to some extend http://dev.mysql.com/tech-resources/articles/xml-in-mysql5.1-6.0.html I used that myself but the query can get unreadable very quickly ;) Of course it is not "real" XML in terms of DOM or anything but a nice and interessting approach on the topic
DrColossos
Thanx for taking the time to help out! I am not sure if I am getting the right output, I am passing $row to the function, $row = mysql_fetch_array($result); I am assuming that is not correct, but I am not sure how I can get mySQL to export the users info in such a manner so that each users info is inside of an array, inside the main array, or am I misunderstanding it?
@kielie just exchange the `foreach` loop with your `while` loop and remove the `array` typehint from the function so you can pass in the `$result` from your question example instead.
Gordon
@Gordon thanx so much for the effort! I got it to work!