http://www.perlmonks.org?node_id=31565

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?

Why NOT use XML::Writer?

Related modules

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

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

Replies are listed 'Best First'.
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.
      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.
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
      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.
        This requires IO::String, but this is one method to put the value of the output into a string: my $xml; my $output = IO::String->new($xml); my $w = new XML::Writer(OUTPUT => $output, ... etc
        Even smart people are dumb in most things...
Re: XML::Writer
by metaperl (Curate) on Apr 26, 2010 at 14:44 UTC
      I want to edit an existing tag,delete a tag,add a tag on an existing XML file,which module is suitable for this?
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

      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?).

      We could use this:
      $writer->emptyTag('game', 'name' => 'chess', 'players' => '3');
Re: XML::Writer
by Anonymous Monk on Dec 29, 2013 at 14:49 UTC

    how do we code to obtain the xml tag in below format

    <Title lang="pt-BR">Caçadores de Fantasmas</Title> <Title lang="en-US">Caçadores de Fantasmas - T04-Ep07</Title>