Using Horde_Xml_Element to quickly generate XML from arrays

Horde's Xml_Element package, part of the PHP 5 Horde 4 framework, is a SimpleXML-like wrapper around the DOM extension that gives programmers a midpoint between DOM's power and SimpleXML's simplicity. Most of the syntax is just like SimpleXML - access attributes as array offsets and elements with object accessors - but you can also add new nested elements with object syntax and use namespace prefixes in element names. The biggest extra feature, though, is probably the fromArray() method.

Horde_Xml_Element::fromArray lets you very easily convert an array into a Horde_Xml_Element object, which you can then work with or dump directly to XML:
<?php

// To populate an <entry> element:
$e = new Horde_Xml_Element('<entry/>');
$e->fromArray(array(
    'id' => 1,
    'title' => 'Hi neighbor!',
    'author' => array('name' => 'Chuck', 'email' => 'chuck@horde.org'),
));
echo $e->saveXmlFragment();
Produces:
<entry><id>1</id><title>Hi neighbor!</title><author><name>Chuck</name><email>chuck@horde.org</email></author></entry>
With the Horde/Feed package, which uses Horde_Xml_Element internally, this can be simplified further:
<?php

$e = new Horde_Feed_Entry_Atom(array(
    'id' => 1,
    'title' => 'Hi neighbor!',
    'author' => array('name' => 'Chuck', 'email' => 'chuck@horde.org'),
));
echo $e->saveXmlFragment();
Which produces the same output. Horde_Xml_Element will also let you set attributes and namespaces with a simple syntax. "#" denotes an attribute, and ":" separates namespaces:
$e = new Horde_Xml_Element('<atom:feed/>');
$e->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
$e->fromArray(array(
    'atom:entry' => array(
        'atom:link' => array(
            '#rel' => 'self',
            '#title' => 'Feed title',
            '#dc:date' => '2011-01-31 01:00:00',
            '#href' => 'http://planet.horde.org/atom/',
        ),
    ),
));
echo $e->saveXmlFragment(true) . "\n";

Which produces:

<atom:feed xmlns:atom="http://www.w3.org/2005/Atom">
  <atom:entry>
    <atom:link xmlns:dc="http://purl.org/dc/elements/1.1/" rel="self" title="Feed title" dc:date="2011-01-31 01:00:00" href="http://planet.horde.org/atom/"/>
  </atom:entry>
</atom:feed>