Previous Section  < Day Day Up >  Next Section

11.2 Generating an XML Document

SimpleXML is good for parsing existing XML documents, but you can't use it to create a new one from scratch. For many XML documents, the easiest way to generate them is to build a PHP array whose structure mirrors that of the XML document and then to iterate through the array, printing each element with appropriate formatting.

Example 11-17 generates the XML for the channel part of an RSS feed using the information in the $channel array.

Example 11-17. Generating XML from an array
$channel = array('title' => "What's For Dinner",

                 'link' => 'http://menu.example.com/',

                 'description' => 'These are your choices of what to eat tonight.');



print "<channel>\n";

foreach ($channel as $element => $content) {

    print " <$element>";

    print htmlentities($content);

    print "</$element>\n";

}

print "</channel>";

Example 11-17 prints:

<channel>

 <title>What's For Dinner</title>

 <link>http://menu.example.com/</link>

 <description>These are your choices of what to eat tonight.</description>

</channel>

Any text content of XML elements must be encoded by htmlentities( ) before it is printed. Just as characters such as < and > have special meaning in HTML, they also have special meaning in XML.

You can use a similar technique to generate XML from information that you retrieve from a database table. Example 11-18 makes an XML representation of the data about spicy dishes.

Example 11-18. Formatting information from a database table as XML
require 'DB.php';

$db = DB::connect('mysql://hunter:w)mp3s@db.example.com/restaurant');



// Change the fetch mode to string-keyed arrays

$db->setFetchMode(DB_FETCHMODE_ASSOC);



print "<dishes>\n";

$q = $db->query("SELECT dish_id, dish_name, price FROM dishes WHERE is_spicy = 1");

while($row = $q->fetchRow( )) {

    print ' <dish id="' . htmlentities($row['dish_id']) .'">' . "\n";

    print '  <name>' . htmlentities($row['dish_name'])."</name>\n";

    print '  <price>' . htmlentities($row['price'])."</price>\n";

    print " </dish>\n";

}

print '</dishes>';

Example 11-18 prints:

<dishes>

 <dish id="4">

  <name>Eggplant with Chili Sauce</name>

  <price>6.50</price>

 </dish>

 <dish id="6">

  <name>General Tso's Chicken</name>

  <price>5.50</price>

 </dish>

</dishes>

If you need to generate more complicated XML documents, investigate PHP 5's DOM functions. They require you to write longer programs than the examples in this section but give you more precise, structured control over all aspects of your XML. Some DOM functions are described briefly in Section 13.9. You can read about them in more detail in Chapter 5 of Upgrading to PHP 5 and the DOM XML section of the PHP Manual (http://www.php.net/domxml).

More complicated XML processing is possible with PHP. Section 13.9 gives some examples, including XSLT transformation. You can read about SOAP and Web Services in PHP at http://www.zend.com/php5/articles/php5-SOAP.php and in Chapter 7 of Essential PHP Tools by David Sklar (Apress).

    Previous Section  < Day Day Up >  Next Section