Skip to content
ccazette edited this page Jan 6, 2012 · 10 revisions

Some key features :
- Extendible, make-your-own drivers to support any XML standard (RSS, RSS2, Atom, XRDS, Podcast RSS, GData, …). Currently a few drivers are in the module to show how you can implement your own-brewed driver. Drivers can filter values and sort out namespaces on-the-fly, but you can also create your own functions (you could easily implement a get_publisher() method in the atom driver for instance), and – if you need – to change the behavior of the main XML class as they all extend it.

- The main XML class that each driver extends allow you to generate XML from array, array from XML, seemlessly add nodes, and play with them easily. A few examples :

// This generates an instance of the atom driver (if you want to use basic XML, just use XML::factory(NULL, "myrootnode");)
$xml = XML::factory("atom");
// Classic node addition
// In this example the title node is configured to be in the "osearch" namespace in the driver, so this will be added on the fly to the XML document.
// Attribute namespaces are also handled
$xml->add_node("osearch:title", "some title", array("attribute1" => "value"));
// The atom driver is configured to set timestamp in Atom date format, but you can format values as you wish using filters
$xml->add_node("updated", time());
// Nodes addition create new instances of XML, so you get access to the same functions and can deal with child nodes as if they were proper XML documents
$author = $xml->add_node("author");
// You can feed any XML instance with an array. Multi-dimensional arrays are supported, as well as having multiple nodes with the same name.
// Note that filters and namespaces are still handled, here the URI will be translated in the URN namespace
$author->from_array(array("uri" => "3324324531", "name" => "John Smith"));
// Render the document allows to pass an argument if you want a nicely indented and formatted XML echo $xml->render(TRUE);

Would output, regarding your Atom driver configuration :

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:osearch="http://a9.com/-/spec/opensearch/1.1">
   <osearch:title attribute1="value">some title</osearch:title>
   <updated>2010-03-29T04:21:30+02:00</updated>
   <author>
      <uri>urn:guid:3324324531</uri>
      <name>John Smith</name>
   </author>
</feed>

Now you have you XML generated (you can also generate it from a string or file, or a mix of all), you can play with it :

// Get the author name 
$name = $xml->author->name 
// Add a node to the author 
$xml->author->add_node("firstname", "John"); 
// Export as array. You can deal with as you wish (or not) and give it back to XML of course. 
$array = $xml->as_array(); 
// You can make more sophisticated queries with get() and xpath() but the example here is not very relevant 
// You can save the data 
$xml->save("test.xml"); 
// Ingest it back in RSS 
$rss = XML::factory("rss", "test.xml");

That’s basically the idea. All in all the lib is a convenient wrapper around PHP DOM. I did not have much time to document and this is an initial release but should get you started.
I won’t have time to support it extensively although I will take it to something satisfying, so if anyone is interested in working on it too, please step ahead !

Clone this wiki locally