1Dumper(3) User Contributed Perl Documentation Dumper(3)
2
3
4
6 XML::Dumper - Perl module for dumping Perl objects from/to XML
7
9 # ===== Using an object
10 use XML::Dumper;
11 $dump = new XML::Dumper;
12
13 $xml = $dump->pl2xml( $perl );
14 $perl = $dump->xml2pl( $xml );
15 $dump->pl2xml( $perl, "my_perl_data.xml.gz" );
16
17 # ===== Using function calls
18 use XML::Dumper;
19
20 $xml = pl2xml( $perl );
21 $perl = xml2pl( $xml );
22
24 use XML::Dumper;
25 my $dump = new XML::Dumper;
26
27 my $perl = '';
28 my $xml = '';
29
30 # ===== Convert Perl code to XML
31 $perl = [
32 {
33 fname => 'Fred',
34 lname => 'Flintstone',
35 residence => 'Bedrock'
36 },
37 {
38 fname => 'Barney',
39 lname => 'Rubble',
40 residence => 'Bedrock'
41 }
42 ];
43 $xml = $dump->pl2xml( $perl );
44
45 # ===== Dump to a file
46 my $file = "dump.xml";
47 $dump->pl2xml( $perl, $file );
48
49 # ===== Convert XML to Perl code
50 $xml = q⎪
51 <perldata>
52 <arrayref>
53 <item key="0">
54 <hashref>
55 <item key="fname">Fred</item>
56 <item key="lname">Flintstone</item>
57 <item key="residence">Bedrock</item>
58 </hashref>
59 </item>
60 <item key="1">
61 <hashref>
62 <item key="fname">Barney</item>
63 <item key="lname">Rubble</item>
64 <item key="residence">Bedrock</item>
65 </hashref>
66 </item>
67 </arrayref>
68 </perldata>
69 ⎪;
70
71 my $perl = $dump->xml2pl( $xml );
72
73 # ===== Convert an XML file to Perl code
74 my $perl = $dump->xml2pl( $file );
75
76 # ===== And serialize Perl code to an XML file
77 $dump->pl2xml( $perl, $file );
78
79 # ===== USE COMPRESSION
80 $dump->pl2xml( $perl, $file.".gz" );
81
82 # ===== INCLUDE AN IN-DOCUMENT DTD
83 $dump->dtd;
84 my $xml_with_dtd = $dump->pl2xml( $perl );
85
86 # ===== USE EXTERNAL DTD
87 $dump->dtd( $file, $url );
88 my $xml_with_link_to_dtd = $dump->pl2xml( $perl );
89
91 XML::Dumper dumps Perl data to XML format. XML::Dumper can also read
92 XML data that was previously dumped by the module and convert it back
93 to Perl. You can use the module read the XML from a file and write the
94 XML to a file. Perl objects are blessed back to their original packag‐
95 ing; if the modules are installed on the system where the perl objects
96 are reconstituted from xml, they will behave as expected. Intuitively,
97 if the perl objects are converted and reconstituted in the same envi‐
98 ronment, all should be well. And it is.
99
100 Additionally, because XML benefits so nicely from compression,
101 XML::Dumper understands gzipped XML files. It does so with an optional
102 dependency on Compress::Zlib. So, if you dump a Perl variable with a
103 file that has an extension of '.xml.gz', it will store and compress the
104 file in gzipped format. Likewise, if you read a file with the exten‐
105 sion '.xml.gz', it will uncompress the file in memory before parsing
106 the XML back into a Perl variable.
107
108 Another fine challenge that this module rises to meet is that it under‐
109 stands circular definitions and multiple references to a single object.
110 This includes doubly-linked lists, circular references, and the so-
111 called 'Flyweight' pattern of Object Oriented programming. So it can
112 take the gnarliest of your perl data, and should do just fine.
113
114 One caveat; XML::Dumper does not handle binary data. There have been
115 discussions in the expat mailing list archives discussing the chal‐
116 lenges associated with encoding binary data with XML. I chose the cow‐
117 ardly path of making the problem a non-issue by not addressing it. To
118 store binary data, one could encode the data into ASCII before encapsu‐
119 lating the data as XML, and then reverse the process to restore the
120 data. There are several Perl modules that one can use for this, Con‐
121 vert::UU, for example.
122
123 FUNCTIONS AND METHODS
124
125 * new() - XML::Dumper constructor.
126 Creates a lean, mean, XML dumping machine. It's also completely at
127 your disposal.
128
129 * dtd -
130 Generates a Document Type Dictionary for the 'perldata' data type.
131 The default behaviour is to embed the DTD in the XML, thereby cre‐
132 ating valid XML. Given a filename, the DTD will be written out to
133 that file and the XML document for your Perl data will link to the
134 file. Given a filename and an URL, the DTD will be written out the
135 file and the XML document will link to the URL. XML::Dumper
136 doesn't try really hard to determine where your DTD's ought to go
137 or relative paths or anything, so be careful with what arguments
138 you supply this method, or just go with the default with the embed‐
139 ded DTD. Between DTD's and Schemas, the potential for more free-
140 form data to be imported and exported becomes feasible.
141
142 Usage:
143
144 dtd(); # Causes XML to include embedded DTD
145 dtd( $file ); # DTD saved to $file; XML will link to $file
146 dtd( $file, $url ); # DTD saved to $file; XML will link to $url
147 dtd( 0 ); # Prevents XML from including embedded DTD
148
149 * pl2xml( $xml, [ $file ] ) -
150 (Also perl2xml(), for those who enjoy readability over brevity).
151
152 Converts Perl data to XML. If a second argument is given, then the
153 Perl data will be stored to disk as XML, using the second argument
154 as a filename.
155
156 Usage: See Synopsis
157
158 * xml2pl( $xml_or_filename, [ $callback ] ) -
159 (Also xml2perl(), for those who enjoy readability over brevity.)
160
161 Converts XML to a Perl datatype. If this method is given a second
162 argument, XML::Dumper will use the second argument as a callback
163 (if possible). If the first argument isn't XML and exists as a
164 file, that file will be read and its contents will be used as the
165 input XML.
166
167 Currently, the only supported invocation of callbacks is through
168 soft references. That is to say, the callback argument ought to be
169 a string that matches the name of a callable method for your
170 classes. If you have a congruent interface, this should work like a
171 peach. If your class interface doesn't have such a named method, it
172 won't be called.
173
174 * xml_compare( $xml1, $xml2 ) - Compares xml for content
175 Compares two dumped Perl data structures (that is, compares the
176 xml) for identity in content. Use this function rather than perl's
177 built-in string comparison. This function will return true for any
178 two perl data that are either deep clones of each other, or identi‐
179 cal. This method is exported by default.
180
181 * xml_identity( $xml1, $xml2 ) - Compares xml for identity
182 Compares two dumped Perl data structures (that is, compares the
183 xml) for identity in instantiation. This function will return true
184 for any two perl data that are identical, but not for deep clones
185 of each other. This method is also exported by default.
186
188 By default, the following methods are exported:
189
190 xml2pl, pl2xml, xml_compare, xml_identity
191
193 XML::Dumper has changed API since 0.4, as a response to a bug report
194 from PerlMonks. I felt it was necessary, as the functions simply didn't
195 work as advertised. That is, xml2pl really didnt accept xml as an argu‐
196 ment; what it wanted was an XML Parse tree. To correct for the API
197 change, simply don't parse the XML before feeding it to XML::Dumper.
198
199 XML::Dumper also has no understanding of typeglobs (references or not),
200 references to regular expressions, or references to Perl subroutines.
201 Turns out that Data::Dumper doesn't do references to Perl subroutines,
202 either, so at least I'm in somewhat good company.
203
204 XML::Dumper requires one perl module, available from CPAN
205
206 XML::Parser
207
208 XML::Parser itself relies on Clark Cooper's Expat implementation in
209 Perl, which in turn requires James Clark's expat package itself. See
210 the documentation for XML::Parser for more information.
211
213 The list of credits got so long that I had to move it to the Changes
214 file. Thanks to all those who've contributed with bug reports and sug‐
215 gested features! Keep 'em coming!
216
217 I've had ownership of the module since June of 2002, and very much
218 appreciate requests on how to make the module better. It has served me
219 well, both as a learning tool on how I can repay my debt to the Perl
220 Community, and as a practical module that is useful. I'm thrilled to be
221 able to offer this bit of code. So, if you have suggestions, bug
222 reports, or feature requests, please let me know and I'll do my best to
223 make this a better module.
224
226 Mike Wong <mike_w3@pacbell.net>
227
228 XML::Dumper is free software. You can redistribute it and/or modify it
229 under the same terms as Perl itself.
230
232 Jonathan Eisenzopf <eisen@pobox.com>
233
235 perl(1) Compress::Zlib(3) XML::Parser(3) Data::DumpXML(3)
236
237
238
239perl v5.8.8 2006-04-05 Dumper(3)