http://qs1969.pair.com?node_id=496522

X-Posted from my use.perl journal (though this meditation is a bit different).

I know many would rather not see another module in the XML namespace, but I've started putting together a proof of concept called XML::Composer (it was originally called XML::JFDI with apologies to Jesse). It will let you write really, really bad XML. It will also allow you to write XML "variants" like the awful Yahoo! IDIF format. Namespaces? It doesn't know about 'em or care about 'em. If you want to use a colon in an attribute name, go ahead. Want to inject an XML snippet from somewhere else? Go ahead. It will simply do whatever you tell it to do. It's a sad fact of life that there's a lot of bad XML "variants" out there that we, as developers, have to live with (maybe this module should be called XML::Awful).

The primary idea is to be able to write XML in just about any format you want. Frequently this means that it will cheerfully produce bad XML, but rather than spend hours scouring the docs only to find yourself reporting another bug, you can quickly and easily tweak what you want. Unfortunately, most of the modules in the XML namespace are geared towards producing correct XML. Sometimes you need to produce bad XML so you roll your own XML generator and hope no one ever sees it.

The aim of XML::Composer is to make bad XML easy to write. If you have to produce it, at least you should produce it as easily as possible.

use XML::Composer; my $xml = XML::Composer->new({ # tag method 'ns:foo' => 'foo', 'bar' => 'bar', 'ns2:baz' => 'baz', }); $xml->Decl; # add declaration (optional) $xml->PI('xml-stylesheet', {type => 'text/xsl', href="$xslt_url"}) +; $xml->foo( { id => 3, 'xmlns:ns2' => $url}, $xml->bar('silly'), $xml->Comment('this is a > comment'), $xml->Raw('<bad tag>'); $xml->baz({'asdf:some_attr' => 'value'}, 'whee!'), ); print $xml->Out; if ($xml->Validate) { # false for above example } __END__ <?xml version="1.0"> <?xml-stylesheet type="text/xsl" href="$xslt_url"?> <ns:foo id="3" xmlns:ns2="$url"> <bar>silly</bar> <!-- this is a &gt; comment --> <bad tag> <ns2:baz asdf:some_attr="value">whee!</ns2:baz> </ns:foo>

This module (not yet on the CPAN) assumes that you, the programmer, know what you're doing. It's designed for agile development: produce something quickly but have a test suite to verify that your output is correct. In short, there is a very specific design philosophy here and I wouldn't recommend it for those who don't have test suites, but it makes coding very fast (and no, it doesn't use AUTOLOAD to build those methods).

Thoughts?

Cheers,
Ovid

New address of my CGI Course.