View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Moritz Sinn > XML-XMLWriter > XML::XMLWriter



Annotate this POD

View/Report Bugs
Module Version: 0.1   Source  


XML::XMLWriter - Module for creating a XML document object oriented with on the fly validating towards the given DTD.


Perl Version


Standard Modules

        Carp 1.01
        Encode 1.98

Nonstandard Modules

        XML::ParseDTD 0.1.3


Example Code


        use XML::XMLWriter;

        my @data=(['Name', 'Adress', 'Email', 'Sex'],
                  ['Herbert', 'BeerAvenue 45', '', 'Male'],
                  ['Anelise', 'SchmidtStreet 21', '', 'Female'],
                  ['XYZ', 'ZYX', 'ZY', 'XZ'],

        my $doc = new XML::XMLWriter(system => '',
                                     public => '-//W3C//DTD XHTML 1.0 Transitional//EN');

        my $html = $doc->createRoot;
        $html->head->title->_pcdata('A Table');
        my $body = $html->body;
        $body->h1->_pcdata('Here is a table!');
        my $table = $body->table({align => 'center', cellspacing => 1, cellpadding => 2, border => 1});
        for(my $i=0; $i<@data; $i++) {
          my $tr = $table->tr;
          foreach $_ (@{$data[$i]}) {
            $i==0 ? $tr->th->_pcdata($_) : $tr->td->_pcdata($_);
        $body->b->_pcdata("that's it!");

Example Output

        <?xml version="1.0" encoding="ISO-8859-15" standalone="no"?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" SYSTEM "">
        <html><head><title>A Table</title></head><body><h1>Here is a table!</h1><table align="center" cellspacing="1" cellpadding="2" border="1"><tr><th>Name</th><th>Adress</th><th>Email</th><th>Sex</th></tr><tr><td>Herbert</td><td>BeerAvenue 45</td><td></td><td>Male</td></tr><tr><td>Anelise</td><td>SchmidtStree 21</td><td></td><td>Female</td></tr><tr><td>XYZ</td><td>ZYX</td><td>ZY</td><td>XZ</td></tr><tr><td>etc...</td></tr></table><b>that's it!</b></body></html>


XMLWriter is a Perl 5 object class, its purpose is to make writing XML documents easier, cleaner, safer and standard conform. Its easier because of the object oriented way XML documents are written with XMLWriter. Its cleaner because of the simple but logical API and its safe and standard conform because of the automatically done checking against the the DTD.

But still: it might be a matter of taste whether one finds XMLWriter usefull or not and it probably has some bugs (i would appreciate a lot if you report them to me), many usefull features are missing, not implemented or not even thought of and perhaps the API with all its simpleness might be confusing though. So please tell me your opinion and tell me the way how you would make XMLWriter better. Its not so easy to develop a good API for this matter.

XMLWriter contains 3 packages: which gives you the document object, which provides the element/tag objects and which represents the parsed character data the document contains. There'll probably come more objects in feature releases. The most interesting class is It provides some methods you can call on every document element, but besides those methods it uses the AUTOLOAD feature of perl to expect every not known method name to be the name of a tag that should be added to the list of child tags of the element the method is called on. So calling $html->head will simply add a new element (the head element) to the list of child tags of the html element. The head object is returned. Have a look at the examples for better understanding. You should also read the POD of and



All methods expect the data you pass them to be encoded in UTF8. So if you take data with diffrent encoding call Encode::decode(yourencoding,$data) to make it UTF8. Finally the whole document will be encoded in the document encoding (default: UTF8) before being returned.

The Constructor

new ([ %conf ])

The only argument can be a hash setting some configuration options:


createRoot ([$rootelem, %arguments, $character_data])

Creates the root element and returns a XML::XMLWriter::Element object representing it.

Possible parameters:

Instead of passing the third argument you can also just do a $root->_pcdata(yourdata), its exactly the same.

get ()

Returns the XML document as a string. Every elements child list is checked and a warning is produce if its not allowed by the DTD.

print ()

Prints the XML document to STDOUT.

get_dtd ()

Returns the internally used XML::ParseDTD object.

syntax highlighting: