Add built local::lib
[catagits/Gitalist.git] / local-lib5 / man / man3 / XML::Simple::FAQ.3pm
1 .\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.10)
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
6 .if t .sp .5v
7 .if n .sp
8 ..
9 .de Vb \" Begin verbatim text
10 .ft CW
11 .nf
12 .ne \\$1
13 ..
14 .de Ve \" End verbatim text
15 .ft R
16 .fi
17 ..
18 .\" Set up some character translations and predefined strings.  \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote.  \*(C+ will
21 .\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
24 .tr \(*W-
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
26 .ie n \{\
27 .    ds -- \(*W-
28 .    ds PI pi
29 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
31 .    ds L" ""
32 .    ds R" ""
33 .    ds C` ""
34 .    ds C' ""
35 'br\}
36 .el\{\
37 .    ds -- \|\(em\|
38 .    ds PI \(*p
39 .    ds L" ``
40 .    ds R" ''
41 'br\}
42 .\"
43 .\" Escape single quotes in literal strings from groff's Unicode transform.
44 .ie \n(.g .ds Aq \(aq
45 .el       .ds Aq '
46 .\"
47 .\" If the F register is turned on, we'll generate index entries on stderr for
48 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
49 .\" entries marked with X<> in POD.  Of course, you'll have to process the
50 .\" output yourself in some meaningful fashion.
51 .ie \nF \{\
52 .    de IX
53 .    tm Index:\\$1\t\\n%\t"\\$2"
54 ..
55 .    nr % 0
56 .    rr F
57 .\}
58 .el \{\
59 .    de IX
60 ..
61 .\}
62 .\"
63 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
64 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
65 .    \" fudge factors for nroff and troff
66 .if n \{\
67 .    ds #H 0
68 .    ds #V .8m
69 .    ds #F .3m
70 .    ds #[ \f1
71 .    ds #] \fP
72 .\}
73 .if t \{\
74 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
75 .    ds #V .6m
76 .    ds #F 0
77 .    ds #[ \&
78 .    ds #] \&
79 .\}
80 .    \" simple accents for nroff and troff
81 .if n \{\
82 .    ds ' \&
83 .    ds ` \&
84 .    ds ^ \&
85 .    ds , \&
86 .    ds ~ ~
87 .    ds /
88 .\}
89 .if t \{\
90 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
91 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
92 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
93 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
94 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
95 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
96 .\}
97 .    \" troff and (daisy-wheel) nroff accents
98 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
99 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
100 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
101 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
102 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
103 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
104 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
105 .ds ae a\h'-(\w'a'u*4/10)'e
106 .ds Ae A\h'-(\w'A'u*4/10)'E
107 .    \" corrections for vroff
108 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
109 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
110 .    \" for low resolution devices (crt and lpr)
111 .if \n(.H>23 .if \n(.V>19 \
112 \{\
113 .    ds : e
114 .    ds 8 ss
115 .    ds o a
116 .    ds d- d\h'-1'\(ga
117 .    ds D- D\h'-1'\(hy
118 .    ds th \o'bp'
119 .    ds Th \o'LP'
120 .    ds ae ae
121 .    ds Ae AE
122 .\}
123 .rm #[ #] #H #V #F C
124 .\" ========================================================================
125 .\"
126 .IX Title "XML::Simple::FAQ 3"
127 .TH XML::Simple::FAQ 3 "2004-11-20" "perl v5.8.7" "User Contributed Perl Documentation"
128 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
129 .\" way too many mistakes in technical documents.
130 .if n .ad l
131 .nh
132 .SH "Frequently Asked Questions about XML::Simple"
133 .IX Header "Frequently Asked Questions about XML::Simple"
134 .SH "Basics"
135 .IX Header "Basics"
136 .SS "What is XML::Simple designed to be used for?"
137 .IX Subsection "What is XML::Simple designed to be used for?"
138 XML::Simple is a Perl module that was originally developed as a tool for
139 reading and writing configuration data in \s-1XML\s0 format.  You can use it for
140 many other purposes that involve storing and retrieving structured data in
141 \&\s-1XML\s0.
142 .PP
143 You might also find XML::Simple a good starting point for playing with \s-1XML\s0
144 from Perl.  It doesn't have a steep learning curve and if you outgrow its
145 capabilities there are plenty of other Perl/XML modules to 'step up' to.
146 .SS "Why store configuration data in \s-1XML\s0 anyway?"
147 .IX Subsection "Why store configuration data in XML anyway?"
148 The many advantages of using \s-1XML\s0 format for configuration data include:
149 .IP "\(bu" 4
150 Using existing \s-1XML\s0 parsing tools requires less development time, is easier
151 and more robust than developing your own config file parsing code
152 .IP "\(bu" 4
153 \&\s-1XML\s0 can represent relationships between pieces of data, such as nesting of
154 sections to arbitrary levels (not easily done with .INI files for example)
155 .IP "\(bu" 4
156 \&\s-1XML\s0 is basically just text, so you can easily edit a config file (easier than
157 editing a Win32 registry)
158 .IP "\(bu" 4
159 \&\s-1XML\s0 provides standard solutions for handling character sets and encoding
160 beyond basic \s-1ASCII\s0 (important for internationalization)
161 .IP "\(bu" 4
162 If it becomes necessary to change your configuration file format, there are
163 many tools available for performing transformations on \s-1XML\s0 files
164 .IP "\(bu" 4
165 \&\s-1XML\s0 is an open standard (the world does not need more proprietary binary
166 file formats)
167 .IP "\(bu" 4
168 Taking the extra step of developing a \s-1DTD\s0 allows the format of configuration
169 files to be validated before your program reads them (not directly supported
170 by XML::Simple)
171 .IP "\(bu" 4
172 Combining a \s-1DTD\s0 with a good \s-1XML\s0 editor can give you a \s-1GUI\s0 config editor for
173 minimal coding effort
174 .SS "What isn't XML::Simple good for?"
175 .IX Subsection "What isn't XML::Simple good for?"
176 The main limitation of XML::Simple is that it does not work with 'mixed
177 content' (see the next question).  If you consider your \s-1XML\s0 files contain
178 marked up text rather than structured data, you should probably use another
179 module.
180 .PP
181 If you are working with very large \s-1XML\s0 files, XML::Simple's approach of
182 representing the whole file in memory as a 'tree' data structure may not be
183 suitable.
184 .SS "What is mixed content?"
185 .IX Subsection "What is mixed content?"
186 Consider this example \s-1XML:\s0
187 .PP
188 .Vb 3
189 \&  <document>
190 \&    <para>This is <em>mixed</em> content.</para>
191 \&  </document>
192 .Ve
193 .PP
194 This is said to be mixed content, because the <para> element contains
195 both character data (text content) and nested elements.
196 .PP
197 Here's some more \s-1XML:\s0
198 .PP
199 .Vb 5
200 \&  <person>
201 \&    <first_name>Joe</first_name>
202 \&    <last_name>Bloggs</last_name>
203 \&    <dob>25\-April\-1969</dob>
204 \&  </person>
205 .Ve
206 .PP
207 This second example is not generally considered to be mixed content.  The
208 <first_name>, <last_name> and <dob> elements contain
209 only character data and the  <person> element contains only nested
210 elements.  (Note: Strictly speaking, the whitespace between the nested
211 elements is character data, but it is ignored by XML::Simple).
212 .SS "Why doesn't XML::Simple handle mixed content?"
213 .IX Subsection "Why doesn't XML::Simple handle mixed content?"
214 Because if it did, it would no longer be simple :\-)
215 .PP
216 Seriously though, there are plenty of excellent modules that allow you to
217 work with mixed content in a variety of ways.  Handling mixed content
218 correctly is not easy and by ignoring these issues, XML::Simple is able to
219 present an \s-1API\s0 without a steep learning curve.
220 .SS "Which Perl modules do handle mixed content?"
221 .IX Subsection "Which Perl modules do handle mixed content?"
222 Every one of them except XML::Simple :\-)
223 .PP
224 If you're looking for a recommendation, I'd suggest you look at the Perl-XML
225 \&\s-1FAQ\s0 at:
226 .PP
227 .Vb 1
228 \&  http://perl\-xml.sourceforge.net/faq/
229 .Ve
230 .SH "Installation"
231 .IX Header "Installation"
232 .SS "How do I install XML::Simple?"
233 .IX Subsection "How do I install XML::Simple?"
234 If you're running ActiveState Perl, you've probably already got XML::Simple 
235 (although you may want to upgrade to version 1.09 or better for \s-1SAX\s0 support).
236 .PP
237 If you do need to install XML::Simple, you'll need to install an \s-1XML\s0 parser
238 module first.  Install either XML::Parser (which you may have already) or
239 \&\s-1XML::SAX\s0.  If you install both, \s-1XML::SAX\s0 will be used by default.
240 .PP
241 Once you have a parser installed ...
242 .PP
243 On Unix systems, try:
244 .PP
245 .Vb 1
246 \&  perl \-MCPAN \-e \*(Aqinstall XML::Simple\*(Aq
247 .Ve
248 .PP
249 If that doesn't work, download the latest distribution from
250 ftp://ftp.cpan.org/pub/CPAN/authors/id/G/GR/GRANTM , unpack it and run these
251 commands:
252 .PP
253 .Vb 4
254 \&  perl Makefile.PL
255 \&  make
256 \&  make test
257 \&  make install
258 .Ve
259 .PP
260 On Win32, if you have a recent build of ActiveState Perl (618 or better) try
261 this command:
262 .PP
263 .Vb 1
264 \&  ppm install XML::Simple
265 .Ve
266 .PP
267 If that doesn't work, you really only need the Simple.pm file, so extract it
268 from the .tar.gz file (eg: using WinZIP) and save it in the \esite\elib\eXML 
269 directory under your Perl installation (typically C:\ePerl).
270 .SS "I'm trying to install XML::Simple and 'make test' fails"
271 .IX Subsection "I'm trying to install XML::Simple and 'make test' fails"
272 Is the directory where you've unpacked XML::Simple mounted from a file server
273 using \s-1NFS\s0, \s-1SMB\s0 or some other network file sharing?  If so, that may cause
274 errors in the the following test scripts:
275 .PP
276 .Vb 3
277 \&  3_Storable.t
278 \&  4_MemShare.t
279 \&  5_MemCopy.t
280 .Ve
281 .PP
282 The test suite is designed to exercise the boundary conditions of all
283 XML::Simple's functionality and these three scripts exercise the caching
284 functions.  If XML::Simple is asked to parse a file for which it has a cached
285 copy of a previous parse, then it compares the timestamp on the \s-1XML\s0 file with
286 the timestamp on the cached copy.  If the cached copy is *newer* then it will
287 be used.  If the cached copy is older or the same age then the file is
288 re-parsed.  The test scripts will get confused by networked filesystems if
289 the workstation and server system clocks are not synchronised (to the
290 second).
291 .PP
292 If you get an error in one of these three test scripts but you don't plan to
293 use the caching options (they're not enabled by default), then go right ahead
294 and run 'make install'.  If you do plan to use caching, then try unpacking
295 the distribution on local disk and doing the build/test there.
296 .PP
297 It's probably not a good idea to use the caching options with networked
298 filesystems in production.  If the file server's clock is ahead of the local
299 clock, XML::Simple will re-parse files when it could have used the cached
300 copy.  However if the local clock is ahead of the file server clock and a
301 file is changed immediately after it is cached, the old cached copy will be
302 used.
303 .PP
304 Is one of the three test scripts (above) failing but you're not running on
305 a network filesystem?  Are you running Win32?  If so, you may be seeing a bug
306 in Win32 where writes to a file do not affect its modfication timestamp.
307 .PP
308 If none of these scenarios match your situation, please confirm you're
309 running the latest version of XML::Simple and then email the output of
310 \&'make test' to me at grantm@cpan.org
311 .SS "Why is XML::Simple so slow?"
312 .IX Subsection "Why is XML::Simple so slow?"
313 If you find that XML::Simple is very slow reading \s-1XML\s0, the most likely reason
314 is that you have \s-1XML::SAX\s0 installed but no additional \s-1SAX\s0 parser module.  The
315 \&\s-1XML::SAX\s0 distribution includes an \s-1XML\s0 parser written entirely in Perl.  This is
316 very portable but not very fast.  For better performance install either
317 XML::SAX::Expat or XML::LibXML.
318 .SH "Usage"
319 .IX Header "Usage"
320 .SS "How do I use XML::Simple?"
321 .IX Subsection "How do I use XML::Simple?"
322 If you had an \s-1XML\s0 document called /etc/appconfig/foo.xml you could 'slurp' it
323 into a simple data structure (typically a hashref) with these lines of code:
324 .PP
325 .Vb 1
326 \&  use XML::Simple;
327 \&
328 \&  my $config = XMLin(\*(Aq/etc/appconfig/foo.xml\*(Aq);
329 .Ve
330 .PP
331 The \fIXMLin()\fR function accepts options after the filename.
332 .SS "There are so many options, which ones do I really need to know about?"
333 .IX Subsection "There are so many options, which ones do I really need to know about?"
334 Although you can get by without using any options, you shouldn't even
335 consider using XML::Simple in production until you know what these two
336 options do:
337 .IP "\(bu" 4
338 forcearray
339 .IP "\(bu" 4
340 keyattr
341 .PP
342 The reason you really need to read about them is because the default values
343 for these options will trip you up if you don't.  Although everyone agrees
344 that these defaults are not ideal, there is not wide agreement on what they
345 should be changed to.  The answer therefore is to read about them (see below)
346 and select values which are right for you.
347 .SS "What is the forcearray option all about?"
348 .IX Subsection "What is the forcearray option all about?"
349 Consider this \s-1XML\s0 in a file called ./person.xml:
350 .PP
351 .Vb 7
352 \&  <person>
353 \&    <first_name>Joe</first_name>
354 \&    <last_name>Bloggs</last_name>
355 \&    <hobbie>bungy jumping</hobbie>
356 \&    <hobbie>sky diving</hobbie>
357 \&    <hobbie>knitting</hobbie>
358 \&  </person>
359 .Ve
360 .PP
361 You could read it in with this line:
362 .PP
363 .Vb 1
364 \&  my $person = XMLin(\*(Aq./person.xml\*(Aq);
365 .Ve
366 .PP
367 Which would give you a data structure like this:
368 .PP
369 .Vb 5
370 \&  $person = {
371 \&    \*(Aqfirst_name\*(Aq => \*(AqJoe\*(Aq,
372 \&    \*(Aqlast_name\*(Aq  => \*(AqBloggs\*(Aq,
373 \&    \*(Aqhobbie\*(Aq     => [ \*(Aqbungy jumping\*(Aq, \*(Aqsky diving\*(Aq, \*(Aqknitting\*(Aq ]
374 \&  };
375 .Ve
376 .PP
377 The <first_name> and <last_name> elements are represented as
378 simple scalar values which you could refer to like this:
379 .PP
380 .Vb 1
381 \&  print "$person\->{first_name} $person\->{last_name}\en";
382 .Ve
383 .PP
384 The <hobbie> elements are represented as an array \- since there is
385 more than one.  You could refer to the first one like this:
386 .PP
387 .Vb 1
388 \&  print $person\->{hobbie}\->[0], "\en";
389 .Ve
390 .PP
391 Or the whole lot like this:
392 .PP
393 .Vb 1
394 \&  print join(\*(Aq, \*(Aq, @{$person\->{hobbie}} ), "\en";
395 .Ve
396 .PP
397 The catch is, that these last two lines of code will only work for people
398 who have more than one hobbie.  If there is only one <hobbie>
399 element, it will be represented as a simple scalar (just like
400 <first_name> and <last_name>).  Which might lead you to write
401 code like this:
402 .PP
403 .Vb 6
404 \&  if(ref($person\->{hobbie})) {
405 \&    print join(\*(Aq, \*(Aq, @{$person\->{hobbie}} ), "\en";
406 \&  }
407 \&  else {
408 \&    print $person\->{hobbie}, "\en";
409 \&  }
410 .Ve
411 .PP
412 Don't do that.
413 .PP
414 One alternative approach is to set the forcearray option to a true value:
415 .PP
416 .Vb 1
417 \&  my $person = XMLin(\*(Aq./person.xml\*(Aq, forcearray => 1);
418 .Ve
419 .PP
420 Which will give you a data structure like this:
421 .PP
422 .Vb 5
423 \&  $person = {
424 \&    \*(Aqfirst_name\*(Aq => [ \*(AqJoe\*(Aq ],
425 \&    \*(Aqlast_name\*(Aq  => [ \*(AqBloggs\*(Aq ],
426 \&    \*(Aqhobbie\*(Aq     => [ \*(Aqbungy jumping\*(Aq, \*(Aqsky diving\*(Aq, \*(Aqknitting\*(Aq ]
427 \&  };
428 .Ve
429 .PP
430 Then you can use this line to refer to all the list of hobbies even if there
431 was only one:
432 .PP
433 .Vb 1
434 \&  print join(\*(Aq, \*(Aq, @{$person\->{hobbie}} ), "\en";
435 .Ve
436 .PP
437 The downside of this approach is that the <first_name> and
438 <last_name> elements will also always be represented as arrays even
439 though there will never be more than one:
440 .PP
441 .Vb 1
442 \&  print "$person\->{first_name}\->[0] $person\->{last_name}\->[0]\en";
443 .Ve
444 .PP
445 This might be \s-1OK\s0 if you change the \s-1XML\s0 to use attributes for things that
446 will always be singular and nested elements for things that may be plural:
447 .PP
448 .Vb 3
449 \&  <person first_name="Jane" last_name="Bloggs">
450 \&    <hobbie>motorcycle maintenance</hobbie>
451 \&  </person>
452 .Ve
453 .PP
454 On the other hand, if you prefer not to use attributes, then you could
455 specify that any <hobbie> elements should always be represented as
456 arrays and all other nested elements should be simple scalar values unless
457 there is more than one:
458 .PP
459 .Vb 1
460 \&  my $person = XMLin(\*(Aq./person.xml\*(Aq, forcearray => [ \*(Aqhobbie\*(Aq ]);
461 .Ve
462 .PP
463 The forcearray option accepts a list of element names which should always
464 be forced to an array representation:
465 .PP
466 .Vb 1
467 \&  forcearray => [ qw(hobbie qualification childs_name) ]
468 .Ve
469 .PP
470 See the XML::Simple manual page for more information.
471 .SS "What is the keyattr option all about?"
472 .IX Subsection "What is the keyattr option all about?"
473 Consider this sample \s-1XML:\s0
474 .PP
475 .Vb 5
476 \&  <catalog>
477 \&    <part partnum="1842334" desc="High pressure flange" price="24.50" />
478 \&    <part partnum="9344675" desc="Threaded gasket"      price="9.25" />
479 \&    <part partnum="5634896" desc="Low voltage washer"   price="12.00" />
480 \&  </catalog>
481 .Ve
482 .PP
483 You could slurp it in with this code:
484 .PP
485 .Vb 1
486 \&  my $catalog = XMLin(\*(Aq./catalog.xml\*(Aq);
487 .Ve
488 .PP
489 Which would return a data structure like this:
490 .PP
491 .Vb 10
492 \&  $catalog = {
493 \&      \*(Aqpart\*(Aq => [
494 \&          {
495 \&            \*(Aqpartnum\*(Aq => \*(Aq1842334\*(Aq,
496 \&            \*(Aqdesc\*(Aq    => \*(AqHigh pressure flange\*(Aq,
497 \&            \*(Aqprice\*(Aq   => \*(Aq24.50\*(Aq
498 \&          },
499 \&          {
500 \&            \*(Aqpartnum\*(Aq => \*(Aq9344675\*(Aq,
501 \&            \*(Aqdesc\*(Aq    => \*(AqThreaded gasket\*(Aq,
502 \&            \*(Aqprice\*(Aq   => \*(Aq9.25\*(Aq
503 \&          },
504 \&          {
505 \&            \*(Aqpartnum\*(Aq => \*(Aq5634896\*(Aq,
506 \&            \*(Aqdesc\*(Aq    => \*(AqLow voltage washer\*(Aq,
507 \&            \*(Aqprice\*(Aq   => \*(Aq12.00\*(Aq
508 \&          }
509 \&      ]
510 \&  };
511 .Ve
512 .PP
513 Then you could access the description of the first part in the catalog
514 with this code:
515 .PP
516 .Vb 1
517 \&  print $catalog\->{part}\->[0]\->{desc}, "\en";
518 .Ve
519 .PP
520 However, if you wanted to access the description of the part with the
521 part number of \*(L"9344675\*(R" then you'd have to code a loop like this:
522 .PP
523 .Vb 6
524 \&  foreach my $part (@{$catalog\->{part}}) {
525 \&    if($part\->{partnum} eq \*(Aq9344675\*(Aq) {
526 \&      print $part\->{desc}, "\en";
527 \&      last;
528 \&    }
529 \&  }
530 .Ve
531 .PP
532 The knowledge that each <part> element has a unique partnum attribute
533 allows you to eliminate this search.  You can pass this knowledge on to
534 XML::Simple like this:
535 .PP
536 .Vb 1
537 \&  my $catalog = XMLin($xml, keyattr => [\*(Aqpartnum\*(Aq]);
538 .Ve
539 .PP
540 Which will return a data structure like this:
541 .PP
542 .Vb 7
543 \&  $catalog = {
544 \&    \*(Aqpart\*(Aq => {
545 \&      \*(Aq5634896\*(Aq => { \*(Aqdesc\*(Aq => \*(AqLow voltage washer\*(Aq,   \*(Aqprice\*(Aq => \*(Aq12.00\*(Aq },
546 \&      \*(Aq1842334\*(Aq => { \*(Aqdesc\*(Aq => \*(AqHigh pressure flange\*(Aq, \*(Aqprice\*(Aq => \*(Aq24.50\*(Aq },
547 \&      \*(Aq9344675\*(Aq => { \*(Aqdesc\*(Aq => \*(AqThreaded gasket\*(Aq,      \*(Aqprice\*(Aq => \*(Aq9.25\*(Aq  }
548 \&    }
549 \&  };
550 .Ve
551 .PP
552 XML::Simple has been able to transform \f(CW$catalog\fR\->{part} from an arrayref to
553 a hashref (keyed on partnum).  This transformation is called 'array folding'.
554 .PP
555 Through the use of array folding, you can now index directly to the
556 description of the part you want:
557 .PP
558 .Vb 1
559 \&  print $catalog\->{part}\->{9344675}\->{desc}, "\en";
560 .Ve
561 .PP
562 The 'keyattr' option also enables array folding when the unique key is in a
563 nested element rather than an attribute.  eg:
564 .PP
565 .Vb 10
566 \&  <catalog>
567 \&    <part>
568 \&      <partnum>1842334</partnum>
569 \&      <desc>High pressure flange</desc>
570 \&      <price>24.50</price>
571 \&    </part>
572 \&    <part>
573 \&      <partnum>9344675</partnum>
574 \&      <desc>Threaded gasket</desc>
575 \&      <price>9.25</price>
576 \&    </part>
577 \&    <part>
578 \&      <partnum>5634896</partnum>
579 \&      <desc>Low voltage washer</desc>
580 \&      <price>12.00</price>
581 \&    </part>
582 \&  </catalog>
583 .Ve
584 .PP
585 See the XML::Simple manual page for more information.
586 .SS "So what's the catch with 'keyattr'?"
587 .IX Subsection "So what's the catch with 'keyattr'?"
588 One thing to watch out for is that you might get array folding even if you
589 don't supply the keyattr option.  The default value for this option is:
590 .PP
591 .Vb 1
592 \&  [ \*(Aqname\*(Aq, \*(Aqkey\*(Aq, \*(Aqid\*(Aq]
593 .Ve
594 .PP
595 Which means if your \s-1XML\s0 elements have a 'name', 'key' or 'id' attribute (or
596 nested element) then they may get folded on those values.  This means that
597 you can take advantage of array folding simply through careful choice of
598 attribute names.  On the hand, if you really don't want array folding at all,
599 you'll need to set 'key attr to an empty list:
600 .PP
601 .Vb 1
602 \&  my $ref = XMLin($xml, keyattr => []);
603 .Ve
604 .PP
605 A second 'gotcha' is that array folding only works on arrays.  That might
606 seem obvious, but if there's only one record in your \s-1XML\s0 and you didn't set
607 the 'forcearray' option then it won't be represented as an array and
608 consequently won't get folded into a hash.  The moral is that if you're
609 using array folding, you should always turn on the forcearray option.
610 .PP
611 You probably want to be as specific as you can be too.  For instance, the
612 safest way to parse the <catalog> example above would be:
613 .PP
614 .Vb 2
615 \&  my $catalog = XMLin($xml, keyattr => { part => \*(Aqpartnum\*(Aq},
616 \&                            forcearray => [\*(Aqpart\*(Aq]);
617 .Ve
618 .PP
619 By using the hashref for keyattr, you can specify that only <part>
620 elements should be folded on the 'partnum' attribute (and that the
621 <part> elements should not be folded on any other attribute).
622 .PP
623 By supplying a list of element names for forcearray, you're ensuring that
624 folding will work even if there's only one <part>.  You're also
625 ensuring that if the 'partnum' unique key is supplied in a nested element
626 then that element won't get forced to an array too.
627 .SS "How do I know what my data structure should look like?"
628 .IX Subsection "How do I know what my data structure should look like?"
629 The rules are fairly straightforward:
630 .IP "\(bu" 4
631 each element gets represented as a hash
632 .IP "\(bu" 4
633 unless it contains only text, in which case it'll be a simple scalar value
634 .IP "\(bu" 4
635 or unless there's more than one element with the same name, in which case
636 they'll be represented as an array
637 .IP "\(bu" 4
638 unless you've got array folding enabled, in which case they'll be folded into
639 a hash
640 .IP "\(bu" 4
641 empty elements (no text contents \fBand\fR no attributes) will either be
642 represented as an empty hash, an empty string or undef \- depending on the value
643 of the 'suppressempty' option.
644 .PP
645 If you're in any doubt, use Data::Dumper, eg:
646 .PP
647 .Vb 2
648 \&  use XML::Simple;
649 \&  use Data::Dumper;
650 \&  
651 \&  my $ref = XMLin($xml);
652 \&
653 \&  print Dumper($ref);
654 .Ve
655 .SS "I'm getting 'Use of uninitialized value' warnings"
656 .IX Subsection "I'm getting 'Use of uninitialized value' warnings"
657 You're probably trying to index into a non-existant hash key \- try
658 Data::Dumper.
659 .SS "I'm getting a 'Not an \s-1ARRAY\s0 reference' error"
660 .IX Subsection "I'm getting a 'Not an ARRAY reference' error"
661 Something that you expect to be an array is not.  The two most likely causes
662 are that you forgot to use 'forcearray' or that the array got folded into a
663 hash \- try Data::Dumper.
664 .SS "I'm getting a 'No such array field' error"
665 .IX Subsection "I'm getting a 'No such array field' error"
666 Something that you expect to be a hash is actually an array.  Perhaps array
667 folding failed because one element was missing the key attribute \- try
668 Data::Dumper.
669 .SS "I'm getting an 'Out of memory' error"
670 .IX Subsection "I'm getting an 'Out of memory' error"
671 Something in the data structure is not as you expect and Perl may be trying
672 unsuccessfully to autovivify things \- try Data::Dumper.
673 .PP
674 If you're already using Data::Dumper, try calling \fIDumper()\fR immediately after
675 \&\fIXMLin()\fR \- ie: before you attempt to access anything in the data structure.
676 .SS "My element order is getting jumbled up"
677 .IX Subsection "My element order is getting jumbled up"
678 If you read an \s-1XML\s0 file with \fIXMLin()\fR and then write it back out with
679 \&\fIXMLout()\fR, the order of the elements will likely be different.  (However, if
680 you read the file back in with \fIXMLin()\fR you'll get the same Perl data
681 structure).
682 .PP
683 The reordering happens because XML::Simple uses hashrefs to store your data
684 and Perl hashes do not really have any order.
685 .PP
686 It is possible that a future version of XML::Simple will use Tie::IxHash
687 to store the data in hashrefs which do retain the order.  However this will
688 not fix all cases of element order being lost.
689 .PP
690 If your application really is sensitive to element order, don't use
691 XML::Simple (and don't put order-sensitive values in attributes).
692 .SS "XML::Simple turns nested elements into attributes"
693 .IX Subsection "XML::Simple turns nested elements into attributes"
694 If you read an \s-1XML\s0 file with \fIXMLin()\fR and then write it back out with
695 \&\fIXMLout()\fR, some data which was originally stored in nested elements may end up
696 in attributes.  (However, if you read the file back in with \fIXMLin()\fR you'll
697 get the same Perl data structure).
698 .PP
699 There are a number of ways you might handle this:
700 .IP "\(bu" 4
701 use the 'forcearray' option with \fIXMLin()\fR
702 .IP "\(bu" 4
703 use the 'noattr' option with \fIXMLout()\fR
704 .IP "\(bu" 4
705 live with it
706 .IP "\(bu" 4
707 don't use XML::Simple
708 .SS "Why does \fIXMLout()\fP insert <name> elements (or attributes)?"
709 .IX Subsection "Why does XMLout() insert <name> elements (or attributes)?"
710 Try setting keyattr => [].
711 .PP
712 When you call \fIXMLin()\fR to read \s-1XML\s0, the 'keyattr' option controls whether arrays
713 get 'folded' into hashes.  Similarly, when you call \fIXMLout()\fR, the 'keyattr'
714 option controls whether hashes get 'unfolded' into arrays.  As described above,
715 \&'keyattr' is enabled by default.
716 .SS "Why are empty elements represented as empty hashes?"
717 .IX Subsection "Why are empty elements represented as empty hashes?"
718 An element is always represented as a hash unless it contains only text, in
719 which case it is represented as a scalar string.
720 .PP
721 If you would prefer empty elements to be represented as empty strings or the
722 undefined value, set the 'suppressempty' option to '' or undef respectively.
723 .SS "Why is ParserOpts deprecated?"
724 .IX Subsection "Why is ParserOpts deprecated?"
725 The \f(CW\*(C`ParserOpts\*(C'\fR option is a remnant of the time when XML::Simple only worked
726 with the XML::Parser \s-1API\s0.  Its value is completely ignored if you're using a
727 \&\s-1SAX\s0 parser, so writing code which relied on it would bar you from taking
728 advantage of \s-1SAX\s0.
729 .PP
730 Even if you are using XML::Parser, it is seldom necessary to pass options to
731 the parser object.  A number of people have written to say they use this option
732 to set XML::Parser's \f(CW\*(C`ProtocolEncoding\*(C'\fR option.  Don't do that, it's wrong,
733 Wrong, \s-1WRONG\s0!  Fix the \s-1XML\s0 document so that it's well-formed and you won't have
734 a problem.
735 .PP
736 Having said all of that, as long as XML::Simple continues to support the
737 XML::Parser \s-1API\s0, this option will not be removed.  There are currently no plans
738 to remove support for the XML::Parser \s-1API\s0.