DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

XML::Dumper



NAME

XML::Dumper - Perl module for dumping Perl objects from/to XML


SYNOPSIS

  # ===== Using an object
  use XML::Dumper;
  $dump = new XML::Dumper;
  $xml  = $dump->pl2xml( $perl );
  $perl = $dump->xml2pl( $xml );
  $dump->pl2xml( $perl, "my_perl_data.xml.gz" );
  # ===== Using function calls
  use XML::Dumper;
  $xml  = pl2xml( $perl );
  $perl = xml2pl( $xml );


EXTENDED SYNOPSIS

  use XML::Dumper;
  my $dump = new XML::Dumper;
  my $perl  = '';
  my $xml   = '';
  # ===== Convert Perl code to XML
  $perl = [
    {
                fname       => 'Fred',
                lname       => 'Flintstone',
                residence   => 'Bedrock'
    },
    {
                fname       => 'Barney',
                lname       => 'Rubble',
                residence   => 'Bedrock'
    }
  ];
  $xml = $dump->pl2xml( $perl );
  # ===== Dump to a file
  my $file = "dump.xml";
  $dump->pl2xml( $perl, $file );
  # ===== Convert XML to Perl code
  $xml = q|
  <perldata>
   <arrayref>
    <item key="0">
     <hashref>
        <item key="fname">Fred</item>
        <item key="lname">Flintstone</item>
        <item key="residence">Bedrock</item>
     </hashref>
    </item>
    <item key="1">
     <hashref>
        <item key="fname">Barney</item>
        <item key="lname">Rubble</item>
        <item key="residence">Bedrock</item>
     </hashref>
    </item>
   </arrayref>
  </perldata>
  |;
  my $perl = $dump->xml2pl( $xml );
  # ===== Convert an XML file to Perl code
  my $perl = $dump->xml2pl( $file );
  
  # ===== And serialize Perl code to an XML file
  $dump->pl2xml( $perl, $file );
  # ===== USE COMPRESSION
  $dump->pl2xml( $perl, $file.".gz" );
  # ===== INCLUDE AN IN-DOCUMENT DTD
  $dump->dtd;
  my $xml_with_dtd = $dump->pl2xml( $perl );
  # ===== USE EXTERNAL DTD
  $dump->dtd( $file, $url );
  my $xml_with_link_to_dtd = $dump->pl2xml( $perl );


DESCRIPTION

XML::Dumper dumps Perl data to XML format. XML::Dumper can also read XML data that was previously dumped by the module and convert it back to Perl. You can use the module read the XML from a file and write the XML to a file. Perl objects are blessed back to their original packaging; if the modules are installed on the system where the perl objects are reconstituted from xml, they will behave as expected. Intuitively, if the perl objects are converted and reconstituted in the same environment, all should be well. And it is.

Additionally, because XML benefits so nicely from compression, XML::Dumper understands gzipped XML files. It does so with an optional dependency on Compress::Zlib. So, if you dump a Perl variable with a file that has an extension of '.xml.gz', it will store and compress the file in gzipped format. Likewise, if you read a file with the extension '.xml.gz', it will uncompress the file in memory before parsing the XML back into a Perl variable.

Another fine challenge that this module rises to meet is that it understands circular definitions and multiple references to a single object. This includes doubly-linked lists, circular references, and the so-called 'Flyweight' pattern of Object Oriented programming. So it can take the gnarliest of your perl data, and should do just fine.

One caveat; XML::Dumper does not handle binary data. There have been discussions in the expat mailing list archives discussing the challenges associated with encoding binary data with XML. I chose the cowardly path of making the problem a non-issue by not addressing it. To store binary data, one could encode the data into ASCII before encapsulating the data as XML, and then reverse the process to restore the data. There are several Perl modules that one can use for this, Convert::UU, for example.

FUNCTIONS AND METHODS


EXPORTS

By default, the following methods are exported:

  xml2pl, pl2xml, xml_compare, xml_identity


BUGS AND DEPENDENCIES

XML::Dumper has changed API since 0.4, as a response to a bug report from PerlMonks. I felt it was necessary, as the functions simply didn't work as advertised. That is, xml2pl really didnt accept xml as an argument; what it wanted was an XML Parse tree. To correct for the API change, simply don't parse the XML before feeding it to XML::Dumper.

XML::Dumper also has no understanding of typeglobs (references or not), references to regular expressions, or references to Perl subroutines. Turns out that Data::Dumper doesn't do references to Perl subroutines, either, so at least I'm in somewhat good company.

XML::Dumper requires one perl module, available from CPAN

        XML::Parser

XML::Parser itself relies on Clark Cooper's Expat implementation in Perl, which in turn requires James Clark's expat package itself. See the documentation for XML::Parser for more information.


REVISIONS AND CREDITS

The list of credits got so long that I had to move it to the Changes file. Thanks to all those who've contributed with bug reports and suggested features! Keep 'em coming!

I've had ownership of the module since June of 2002, and very much appreciate requests on how to make the module better. It has served me well, both as a learning tool on how I can repay my debt to the Perl Community, and as a practical module that is useful. I'm thrilled to be able to offer this bit of code. So, if you have suggestions, bug reports, or feature requests, please let me know and I'll do my best to make this a better module.


CURRENT MAINTAINER

Mike Wong <mike_w3@pacbell.net>

XML::Dumper is free software. You can redistribute it and/or modify it under the same terms as Perl itself.


ORIGINAL AUTHOR

Jonathan Eisenzopf <eisen@pobox.com>


=head1 SEE ALSO

perl(1) Compress::Zlib(3) XML::Parser(3) Data::DumpXML(3)