Back in June I wrote Database Simplicity with Class, a feature focused on organizing your database code into a single, easy to use file. At the time, I promised readers I would produce a follow-up that expands the functionality of that class. Since we have had interesting ideas pop up for features, I have decided to split the new database functions into multiple weekly notebooks, starting with handling XML.

I would like to introduce ConvertRStoXML(). This function takes a recordset and converts it into a simple XML document. Two immediate benefits are noticable by doing this. First, you can open up your data in a common format for other developers to use. Second, you can easily use XSL to style your data by using the loadXML() method of the DomDocument. Implementation

Three simple lines can do the trick for us. Simply get a handle on a recordset using ExecuteReader(), and then pass the recordset into our new function.

$sql = "SELECT firstName FROM tPersons";
$rs = $cDB->ExecuteReader($sql);
$xml = $cDB->ConvertRStoXML($rs, "People", "Person");

You’ll notice the two additional paramters at the end of ConvertRStoXML(). The first of the two represents the node name of the highest level node, while the second stands for the first level child nodes within. The XML string returned would look similar to this:

        Ryan        Kevin

How It Works

The function below was inspired by the ASP version from 4 Guys From Rolla, and I then copied it over to PHP using Tony Maston’s PHP DOM walkthrough.

function ConvertRStoXML($rs, $topLevelNodeName, $rowNodeName) {
    $doc = new DomDocument('1.0');
    $root = $doc->createElement($topLevelNodeName);
    $root = $doc->appendChild($root);
    while ($row = @mysql_fetch_array($rs, MYSQL_ASSOC)) {
        $node = $doc->createElement($rowNodeName);
        $node = $root->appendChild($node);
        foreach ($row as $fieldname => $fieldvalue) {
            $child = $doc->createElement($fieldname);
            $child = $node->appendChild($child);
            $value = $doc->createTextNode($fieldvalue);
            $value = $child->appendChild($value);
    $xml_string = $doc->saveXML();
    return $xml_string;

The function loops through every record and every field of the recordset, and then builds the appropriate nodes. As suggested by the Rolla article, cloneNode() can improve performance. While I haven’t implemented it yet, I would be interested to see how readers have taken functions similar to this and turned them in to optimization animals.

And that is all for XML. From here on out each function in our database class will be tracked, and easily accesible. At the end, the class will be available for download with fresh comments, cleaned up code, and all of the functionality you could ever ask for.

HTML Form Builder
Ryan Campbell

Database Simplicity - XML by Ryan Campbell

This entry was posted 5 years ago and was filed under Notebooks.
Comments are currently closed.


  1. Matthijs · 5 years ago

    Hi Ryan, interesting development. Converting the data to XML has indeed some great possibilities. Looking forward to how this goes on. One question: as I understand correctly, this will only work in PHP5, doesn’t it?

  2. Ryan Campbell · 5 years ago

    Yeah, it will only work in PHP 5. It can be done in PHP 4 (I think). I haven’t gone through this tutorial) yet, but it seems to do the same thing in PHP 4.