Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??


XML::Writer generates XML using an interface similar to 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
  • XML::Writer is quite mature

Why NOT use XML::Writer?

  • another method is more appropriate
  • you don't like!

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.


#!/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

In reply to XML::Writer by mirod

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2023-01-30 14:05 GMT
Find Nodes?
    Voting Booth?

    No recent polls found