I've been toying with a variety of different ways to output data, in addition to my search for a better and decoupled templating system,a nd have been exploring an interesting option.

My idea is similar to the .toString() method found in most java classes, but a toXML method that exports the objects data structure (and it's components) as XML, as a DOMXML object. The idea is, I could do something like this:

PHP Code:
$item = new item($_GET["id"]);
$description $item->toXML();
    
$pi $description->create_processing_instruction
   
(
     
"xml-stylesheet",
     
"type=\"text/xsl\" href=\"item.xsl\""
   
);
    
$elements $description->get_elements_by_tagname('item');
    
$description->insert_before($pi$elements[0]);
   
    
header('Content-Type: text/xml');
    echo 
$description->dump_mem(true); 
which item.xsl would be a stylesheet for converting it to html. (btw, this is php4, I'd probably use the xsl extension in php5 and to the transformation server side). I could imagine that you could additionally apply a stylesheet for plain text, one as a soap resonse, even perhaps use XSL-FO to create a pdf.

I suppose my question is... is this good design? One drawback would seem that every class would need a custom coded toXML() method to generate xml, or one would have to create a decorator object to generate the xml (since one may also export as an array, perhaps for it to be used by another object). The decorator solution does get a bit messy though, and it seems sooner or later you perhaps need to get verbose with how the xml is generated (orjust transform an array into xml, kinda like PEAR::XML_Serializer does).

Thoughts?