|
Item Description: A CGI.pm-like module to write XML
Review Synopsis: Robust and convenient, recommended over print
Description
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.
Why use XML::Writer?
- you are generating XML documents "from scratch"
- you are used to CGI.pm
- XML::Writer is quite mature
Why NOT use XML::Writer?
- another method is more appropriate
- you don't like CGI.pm!
Related modules
XML::ValidWriter and
XML::AutoWriter both aim at emulating XML::Writer
interface:
- 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.
- XML::AutoWriter automatically generates missing start or end tags, based
on the DTD.
XML::Generator and
XML::Handler::YAWriter are 2 other modules doing XML generation
Personal notes
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.
Example
#!/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
Re: XML::Writer by Missing Words (Scribe) on Jun 11, 2003 at 00:25 UTC |
Nice review of this module. However, when I went to compile your example code I found a small error.
I believe the last line should read:
$doc->close();
With this change the code compiled fine, and produced the expected output. Regardless of the small error, good review--short and to the point. | [reply] [d/l] |
|
When I try using xml::writer, part of my xml doc gets written then the program just hangs, I have tried flushing the buffer. This seems to work in allowing my script to write more data to the xml file. Even though it goes further it still hangs.
| [reply] |
Re: XML::Writer by Anonymous Monk on May 25, 2007 at 16:54 UTC |
Good review, and very helpful.
One question though. Rather than writing to a file is it possible to simply write to a variable. I've been trying to determine how to do this, and I've had no luck.
Thanks | [reply] |
|
I don't have the answer to your specific question however you might take a look at XML::API which does create/return strings of xml.
| [reply] |
|
| [reply] |
Re: XML::Writer by arunapad (Initiate) on Apr 25, 2010 at 08:42 UTC |
The above was good for reference ..
i had to generate XML in the following format:
<games>
<game name="chess" players="3" \>
<games> ...
the <game > tag doesnt have an end tag </game> .. how can i do this using XML writer ???
it would be helpful if u could reply | [reply] |
|
I can try being helpful, but without your code, it is not that easy...
That said, are you sure the game tag is not <game name="chess" players="3" /> (with a slash instead of a backslash)? In that case it doesn't need a closing tag. The / before the closing angle bracket means that the tag is empty and should be treated as both an opening and a closing tag. As far as an XML parser is concerned <game name="chess" players="3" /> is equivalent to <game name="chess" players="3"></game>, so the XML you are generating should be OK (did you try checking it with an XML parser?).
| [reply] [d/l] [select] |
Re: XML::Writer by metaperl (Curate) on Apr 26, 2010 at 14:44 UTC |
| [reply] |
Back to Reviews
|