Keep It Simple, Stupid | |
PerlMonks |
Item Description: A CGI.pm-like module to write XML
Review Synopsis: Robust and convenient, recommended over print
XML::Writer 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.
XML::ValidWriter and XML::AutoWriter both aim at emulating XML::Writer interface:
XML::Generator and XML::Handler::YAWriter are 2 other modules doing XML generation
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;
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.
#!/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 p +ost below
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: XML::Writer
by Missing Words (Scribe) on Jun 11, 2003 at 00:25 UTC | |
by mark838 (Initiate) on Oct 12, 2005 at 06:35 UTC | |
Re: XML::Writer
by Anonymous Monk on May 25, 2007 at 16:54 UTC | |
by Anonymous Monk on Jun 04, 2007 at 15:21 UTC | |
by Devanchya (Beadle) on Aug 21, 2008 at 01:42 UTC | |
Re: XML::Writer
by metaperl (Curate) on Apr 26, 2010 at 14:44 UTC | |
by Anonymous Monk on Aug 23, 2013 at 06:02 UTC | |
Re: XML::Writer
by arunapad (Initiate) on Apr 25, 2010 at 08:42 UTC | |
by mirod (Canon) on Apr 26, 2010 at 09:58 UTC | |
by Anonymous Monk on Jul 01, 2013 at 23:43 UTC | |
Re: XML::Writer
by Anonymous Monk on Dec 29, 2013 at 14:49 UTC |