modulereview
mirod
<h3>Description</h3>
<p><a href="http://theoryx5.uwinnipeg.ca/CPAN/data/XML-Writer/Writer.html">
XML::Writer</a> generates XML using an interface similar to CGI.pm. It
allows various checks to be performed on the document and takes care of
special caracter encoding.</p>
<h3>Why use XML::Writer?</h3>
<ul>
<li>you are generating XML documents "from scratch"</li>
<li>you are used to CGI.pm</li>
<li>XML::Writer is quite mature</li>
</ul>
<h3>Why NOT use XML::Writer?</h3>
<ul>
<li>another method is more appropriate</li>
<li>you don't like CGI.pm!</li>
</ul>
<h3>Related modules</h3>
<p><a href="http://theoryx5.uwinnipeg.ca/CPAN/data/XML-AutoWriter/XML/ValidWriter.html">
XML::ValidWriter</a> and
<a href="http://theoryx5.uwinnipeg.ca/CPAN/data/XML-AutoWriter/XML/AutoWriter.html">
XML::AutoWriter</a> both aim at emulating XML::Writer
interface:</p>
<ul>
<li>XML::ValidWriter performs some checks on the output document. Notably it
checks whether the elements and attributes are declared in the DTD and whether
you are closing the appropriate element.</li>
<li>XML::AutoWriter automatically generates missing start or end tags, based
on the DTD.</li>
</ul>
<p><a href="http://theoryx5.uwinnipeg.ca/CPAN/data/XML-Generator/Generator.html">
XML::Generator</a> and
<a href="http://theoryx5.uwinnipeg.ca/CPAN/data/XML-Handler-YAWriter/YAWriter.html">
XML::Handler::YAWriter</a> are 2 other modules doing XML generation</p>
<h3>Personal notes</h3>
<p>At the moment XML::Writer seems to be the most mature and
efficient module to generate XML. Of course a lot of the
transformation modules such as XML::Simple, XML::DOM and
XML::Twig can also be used;</p>
<p>Of course plain print's can also be used, but I think
that XML::Writer is a lot more convenient and adds
some control over the generated XML.</p>
<h3>Example</h3>
<code>
#!/bin/perl -w
use strict;
use XML::Writer;
use IO;
my $doc = new IO::File(">doc.xml");
my $writer = new XML::Writer(OUTPUT => $doc);
$writer->startTag("doc", class => "simple"); # tag + att
$writer->dataElement( 'title', "Simple XML Document");# text elt
$writer->startTag( "section");
$writer->dataElement( 'title', "Introduction",
no => 1, type => "intro");
$writer->startTag( "para");
$writer->characters( "a text with");
$writer->dataElement( 'bold', "bold");
$writer->characters( " words.");
$writer->endTag( "para");
$writer->endTag(); # close section
$writer->endTag(); # close doc
$writer->end(); # check that the doc
# has only one element
$doc->close(); # fixed (was $output->close(); ) as suggested by the post below</code>
A CGI.pm-like module to write XML
Robust and convenient, recommended over <tt>print</tt>