Author image Иван Бахчеев


Data::Dump::XML - Dump arbitrary data structures as XML::LibXML object


 use Data::Dump::XML;
 my $dumper = Data::Dump::XML->new;
 $xml = $dumper->dump_xml (@list);


Project source code and repository available on


This module completely rewritten from Gisle Aas Data::DumpXML to manage perl structures in XML using interface to gnome libxml2 (package XML::LibXML). Module provides a single method called dump_xml that takes a list of Perl values as its argument. Returned is an XML::LibXML::Document object that represents any Perl data structures passed to the function. Reference loops are handled correctly.

Compatibility with Data::DumpXML is absent.

As an example of the XML documents produced, the following call:

  $a = bless {a => 1, b => {c => [1,2]}}, "Foo";
  $dumper->dump_xml($a)->toString (1);


  <?xml version="1.0" encoding="utf-8"?>
  <data _class="Foo">

Comparing to Data::DumpXML this module generates noticeably more simple XML tree, based on assumption that links in perl can be defined in implicit way, i.e.: explicit: $a->{b}->{c}->[1]; implicit: $a->{b} {c} [1];

And make possible similar xpath expressions: /data/b/c/*[count (preceding-sibling) = 1]

Data::Dump::XML::Parser is a class that can restore data structures dumped by dump_xml().

Configuration variables

The generated XML is influenced by a set of configuration variables. If you modify them, then it is a good idea to localize the effect. For example:

        my $dumper = new Data::Dump::XML {
                # xml configuration
                'encoding'            => 'utf-8',
                'dtd-location'        => '',
                'namespace'           => {},

                # xml tree namespace
                'dump-config'         => 1,
                'root-name'           => 'data',
                'hash-element'        => 'key',
                'array-element'       => 'item',
                'ref-element'         => 'ref',
                'empty-array'         => 'empty-array',
                'empty-hash'          => 'empty-hash',
                'undef'               => 'undef',
                'key-as-hash-element' => 1,
                '@key-as-attribute'   => 1,

                # options
                'sort-keys'           => 0,
                'granted-restore'     => 1,

Data::DumpXML is function-oriented, but this module is rewritten to be object-oriented, thus configuration parameters are passed as hash into constructor.

The variables are:


Encoding of produced document. Default - 'utf-8'.


This variable contains the location of the DTD. If this variable is non-empty, then a <!DOCTYPE ...> is included in the output. The default is "". Usable with key-as-hash-element parameter.


This hash contains the namespace used for the XML elements. Default: disabled use of namespaces.

Namespaces provides as full attribute name and location. Example:

        'namespace' => {
                'xmlns:xsl' => '',
                'xmlns:xi'  => '',

This parameter define name for xml root element.

hash-element, array-element ref-element

This parameters provides names for hash, array items and references.


        'hash-element'  => 'key',
        'array-element' => 'item',
        'ref-element'   => 'ref',

When this parameter is set, then each hash key, correspondending regexp /^(?:[^\d\W]|:)[\w:-]*$/ dumped as:


        instead of 

        <$hashelement name="$keyname">$keyvalue</$hashelement>




XML::LibXML Features

When dumping XML::LibXML::Element objects, it added by childs to current place in document tree.


The content of globs and subroutines are not dumped. They are restored as the strings "** glob **" and "** code **".

LVALUE and IO objects are not dumped at all. They simply disappear from the restored data structure.


Data::DumpXML, XML::Parser, XML::Dumper, Data::Dump, XML::Dump


The Data::Dump::XML module is written by Ivan Baktsheev <>, based on Data::DumpXML.

The Data::Dump module was written by Gisle Aas, based on Data::Dumper by Gurusamy Sarathy <>.

        Copyright 2003-2009 Ivan Baktcheev.
        Copyright 1998-2003 Gisle Aas.
        Copyright 1996-1998 Gurusamy Sarathy.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.