{"id":7193,"date":"2022-12-20T19:35:46","date_gmt":"2022-12-20T22:35:46","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/xmlsimplefaq-man3\/"},"modified":"2022-12-20T19:35:46","modified_gmt":"2022-12-20T22:35:46","slug":"xmlsimplefaq-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/xmlsimplefaq-man3\/","title":{"rendered":"XML::Simple::FAQ (man3)"},"content":{"rendered":"<h1 align=\"center\">XML::Simple::FAQ<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#Basics\">Basics<\/a><br \/> <a href=\"#Installation\">Installation<\/a><br \/> <a href=\"#Usage\">Usage<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">XML::Simple::FAQ \u2212 Frequently Asked Questions about XML::Simple<\/p>\n<h2>Basics <a name=\"Basics\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>What should I use XML::Simple for?<\/b> <br \/> Nothing!<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">It\u2019s as simple as that.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Choose a better module. See Perl XML::LibXML by Example <http:\/\/grantm.github.io\/perl-libxml-by-example\/> for a gentle introduction to XML::LibXML with lots of examples.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>What was XML::Simple designed to be used for?<\/b> <br \/> XML::Simple is a Perl module that was originally developed as a tool for reading and writing configuration data in <small>XML<\/small> format. You could use it for other purposes that involve storing and retrieving structured data in <small>XML<\/small> but it\u2019s likely to be a frustrating experience.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Why store configuration data in <small>XML<\/small> anyway?<\/b> <br \/> It seemed like a good idea at the time. Now, I use and recommend Config::General which uses a format similar to that used by the Apache web server. This is easier to read than <small>XML<\/small> while still allowing advanced concepts such as nested sections.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">At the time XML::Simple was written, the advantages of using <small>XML<\/small> format for configuration data were thought to include:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Using existing <small>XML<\/small> parsing tools requires less development time, is easier and more robust than developing your own config file parsing code<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p><small>XML<\/small> can represent relationships between pieces of data, such as nesting of sections to arbitrary levels (not easily done with .INI files for example)<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p><small>XML<\/small> is basically just text, so you can easily edit a config file (easier than editing a Win32 registry)<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p><small>XML<\/small> provides standard solutions for handling character sets and encoding beyond basic <small>ASCII<\/small> (important for internationalization)<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>If it becomes necessary to change your configuration file format, there are many tools available for performing transformations on <small>XML<\/small> files<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p><small>XML<\/small> is an open standard (the world does not need more proprietary binary file formats)<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Taking the extra step of developing a <small>DTD<\/small> allows the format of configuration files to be validated before your program reads them (not directly supported by XML::Simple)<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Combining a <small>DTD<\/small> with a good <small>XML<\/small> editor can give you a <small>GUI<\/small> config editor for minimal coding effort<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>What isn\u2019t XML::Simple good for?<\/b> <br \/> The main limitation of XML::Simple is that it does not work with \u2019mixed content\u2019 (see the next question). If you consider your <small>XML<\/small> files contain marked up text rather than structured data, you should probably use another module.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If your source <small>XML<\/small> documents change regularly, it\u2019s likely that you will experience intermittent failures. In particular, failure to properly use the ForceArray and KeyAttr options will produce code that works when you get a list of elements with the same name, but fails when there\u2019s only one item in the list. These types of problems can be avoided by not using XML::Simple in the first place.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you are working with very large <small>XML<\/small> files, XML::Simple\u2019s approach of representing the whole file in memory as a \u2019tree\u2019 data structure may not be suitable.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>What is mixed content?<\/b> <br \/> Consider this example <small>XML:<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><document>  <para>This is <em>mixed<\/em> content.<\/para> <\/document><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This is said to be mixed content, because the <para> element contains both character data (text content) and nested elements.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Here\u2019s some more <small>XML:<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><person> <first_name>Joe<\/first_name> <br \/> <last_name>Bloggs<\/last_name> <br \/> <dob>25\u2212April\u22121969<\/dob>  <\/person><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This second example is not generally considered to be mixed content. The <first_name>, <last_name> and <dob> elements contain only character data and the <person> element contains only nested elements. (Note: Strictly speaking, the whitespace between the nested elements is character data, but it is ignored by XML::Simple).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Why doesn\u2019t XML::Simple handle mixed content?<\/b> <br \/> Because if it did, it would no longer be simple :\u2212)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Seriously though, there are plenty of excellent modules that allow you to work with mixed content in a variety of ways. Handling mixed content correctly is not easy and by ignoring these issues, XML::Simple is able to present an <small>API<\/small> without a steep learning curve.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Which Perl modules do handle mixed content?<\/b> <br \/> Every one of them except XML::Simple :\u2212)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you\u2019re looking for a recommendation, I\u2019d suggest you look at the Perl-XML <small>FAQ<\/small> at:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">http:\/\/perl\u2212xml.sourceforge.net\/faq\/<\/p>\n<h2>Installation <a name=\"Installation\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>How do I install XML::Simple?<\/b> <br \/> If you\u2019re running ActiveState Perl, or Strawberry Perl <http:\/\/strawberryperl.com\/> you\u2019ve probably already got XML::Simple and therefore do not need to install it at all. But you probably also have XML::LibXML, which is a much better module, so just use that.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you do need to install XML::Simple, you\u2019ll need to install an <small>XML<\/small> parser module first. Install either XML::Parser (which you may have already) or <small>XML::SAX.<\/small> If you install both, <small>XML::SAX<\/small> will be used by default.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Once you have a parser installed &#8230;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">On Unix systems, try:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">perl \u2212MCPAN \u2212e &#8216;install XML::Simple&#8217;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If that doesn\u2019t work, download the latest distribution from ftp:\/\/ftp.cpan.org\/pub\/CPAN\/authors\/id\/G\/GR\/GRANTM , unpack it and run these commands:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">perl Makefile.PL <br \/> make <br \/> make test <br \/> make install<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">On Win32, if you have a recent build of ActiveState Perl (618 or better) try this command:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">ppm install XML::Simple<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If that doesn\u2019t work, you really only need the Simple.pm file, so extract it from the .tar.gz file (eg: using WinZIP) and save it in the sitelibXML directory under your Perl installation (typically C:Perl).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>I\u2019m trying to install XML::Simple and \u2019make test\u2019 fails<\/b> <br \/> Is the directory where you\u2019ve unpacked XML::Simple mounted from a file server using <small>NFS, SMB<\/small> or some other network file sharing? If so, that may cause errors in the following test scripts:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">3_Storable.t <br \/> 4_MemShare.t <br \/> 5_MemCopy.t<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The test suite is designed to exercise the boundary conditions of all XML::Simple\u2019s functionality and these three scripts exercise the caching functions. If XML::Simple is asked to parse a file for which it has a cached copy of a previous parse, then it compares the timestamp on the <small>XML<\/small> file with the timestamp on the cached copy. If the cached copy is *newer* then it will be used. If the cached copy is older or the same age then the file is re-parsed. The test scripts will get confused by networked filesystems if the workstation and server system clocks are not synchronised (to the second).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you get an error in one of these three test scripts but you don\u2019t plan to use the caching options (they\u2019re not enabled by default), then go right ahead and run \u2019make install\u2019. If you do plan to use caching, then try unpacking the distribution on local disk and doing the build\/test there.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">It\u2019s probably not a good idea to use the caching options with networked filesystems in production. If the file server\u2019s clock is ahead of the local clock, XML::Simple will re-parse files when it could have used the cached copy. However if the local clock is ahead of the file server clock and a file is changed immediately after it is cached, the old cached copy will be used.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Is one of the three test scripts (above) failing but you\u2019re not running on a network filesystem? Are you running Win32? If so, you may be seeing a bug in Win32 where writes to a file do not affect its modification timestamp.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If none of these scenarios match your situation, please confirm you\u2019re running the latest version of XML::Simple and then email the output of \u2019make test\u2019 to me at grantm@cpan.org<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Why is XML::Simple so slow?<\/b> <br \/> If you find that XML::Simple is very slow reading <small>XML,<\/small> the most likely reason is that you have <small>XML::SAX<\/small> installed but no additional <small>SAX<\/small> parser module. The <small>XML::SAX<\/small> distribution includes an <small>XML<\/small> parser written entirely in Perl. This is very portable but not very fast. For better performance install either XML::SAX::Expat or XML::LibXML.<\/p>\n<h2>Usage <a name=\"Usage\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>How do I use XML::Simple?<\/b> <br \/> If you don\u2019t know how to use XML::Simple then the best approach is to learn to use XML::LibXML <http:\/\/grantm.github.io\/perl-libxml-by-example\/> instead. Stop reading this document and use that one instead.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you are determined to use XML::Simple, it come with copious documentation, so read that.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>There are so many options, which ones do I really need to know about?<\/b> <br \/> Although you can get by without using any options, you shouldn\u2019t even consider using XML::Simple in production until you know what these two options do:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"15%\">\n<p style=\"margin-top: 1em\">forcearray<\/p>\n<\/td>\n<td width=\"68%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"15%\">\n<p>keyattr<\/p>\n<\/td>\n<td width=\"68%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">The reason you really need to read about them is because the default values for these options will trip you up if you don\u2019t. Although everyone agrees that these defaults are not ideal, there is not wide agreement on what they should be changed to. The answer therefore is to read about them (see below) and select values which are right for you.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>What is the forcearray option all about?<\/b> <br \/> Consider this <small>XML<\/small> in a file called .\/person.xml:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><person> <first_name>Joe<\/first_name> <br \/> <last_name>Bloggs<\/last_name> <br \/> <hobbie>bungy jumping<\/hobbie> <br \/> <hobbie>sky diving<\/hobbie> <br \/> <hobbie>knitting<\/hobbie>  <\/person><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You could read it in with this line:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $person = XMLin(&#8216;.\/person.xml&#8217;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Which would give you a data structure like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$person = { <br \/> &#8216;first_name&#8217; => &#8216;Joe&#8217;, <br \/> &#8216;last_name&#8217; => &#8216;Bloggs&#8217;, <br \/> &#8216;hobbie&#8217; => [ &#8216;bungy jumping&#8217;, &#8216;sky diving&#8217;, &#8216;knitting&#8217; ] <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <first_name> and <last_name> elements are represented as simple scalar values which you could refer to like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">print &#8220;$person\u2212>{first_name} $person\u2212>{last_name}n&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <hobbie> elements are represented as an array \u2212 since there is more than one. You could refer to the first one like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">print $person\u2212>{hobbie}\u2212>[0], &#8220;n&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Or the whole lot like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">print join(&#8216;, &#8216;, @{$person\u2212>{hobbie}} ), &#8220;n&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The catch is, that these last two lines of code will only work for people who have more than one hobbie. If there is only one <hobbie> element, it will be represented as a simple scalar (just like <first_name> and <last_name>). Which might lead you to write code like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if(ref($person\u2212>{hobbie})) { <br \/> print join(&#8216;, &#8216;, @{$person\u2212>{hobbie}} ), &#8220;n&#8221;; <br \/> } <br \/> else { <br \/> print $person\u2212>{hobbie}, &#8220;n&#8221;; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Don\u2019t do that.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">One alternative approach is to set the forcearray option to a true value:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $person = XMLin(&#8216;.\/person.xml&#8217;, forcearray => 1);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Which will give you a data structure like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$person = { <br \/> &#8216;first_name&#8217; => [ &#8216;Joe&#8217; ], <br \/> &#8216;last_name&#8217; => [ &#8216;Bloggs&#8217; ], <br \/> &#8216;hobbie&#8217; => [ &#8216;bungy jumping&#8217;, &#8216;sky diving&#8217;, &#8216;knitting&#8217; ] <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Then you can use this line to refer to all the list of hobbies even if there was only one:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">print join(&#8216;, &#8216;, @{$person\u2212>{hobbie}} ), &#8220;n&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The downside of this approach is that the <first_name> and <last_name> elements will also always be represented as arrays even though there will never be more than one:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">print &#8220;$person\u2212>{first_name}\u2212>[0] $person\u2212>{last_name}\u2212>[0]n&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This might be <small>OK<\/small> if you change the <small>XML<\/small> to use attributes for things that will always be singular and nested elements for things that may be plural:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><person first_name=\"Jane\" last_name=\"Bloggs\"> <hobbie>motorcycle maintenance<\/hobbie>  <\/person><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">On the other hand, if you prefer not to use attributes, then you could specify that any <hobbie> elements should always be represented as arrays and all other nested elements should be simple scalar values unless there is more than one:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $person = XMLin(&#8216;.\/person.xml&#8217;, forcearray => [ &#8216;hobbie&#8217; ]);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The forcearray option accepts a list of element names which should always be forced to an array representation:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">forcearray => [ qw(hobbie qualification childs_name) ]<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">See the XML::Simple manual page for more information.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>What is the keyattr option all about?<\/b> <br \/> Consider this sample <small>XML:<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><catalog>  <part partnum=\"1842334\" desc=\"High pressure flange\" price=\"24.50\" \/> <part partnum=\"9344675\" desc=\"Threaded gasket\" price=\"9.25\" \/> <part partnum=\"5634896\" desc=\"Low voltage washer\" price=\"12.00\" \/> <\/catalog><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You could slurp it in with this code:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $catalog = XMLin(&#8216;.\/catalog.xml&#8217;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Which would return a data structure like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$catalog = { <br \/> &#8216;part&#8217; => [ <br \/> { <br \/> &#8216;partnum&#8217; => &#8216;1842334&#8217;, <br \/> &#8216;desc&#8217; => &#8216;High pressure flange&#8217;, <br \/> &#8216;price&#8217; => &#8216;24.50&#8217; <br \/> }, <br \/> { <br \/> &#8216;partnum&#8217; => &#8216;9344675&#8217;, <br \/> &#8216;desc&#8217; => &#8216;Threaded gasket&#8217;, <br \/> &#8216;price&#8217; => &#8216;9.25&#8217; <br \/> }, <br \/> { <br \/> &#8216;partnum&#8217; => &#8216;5634896&#8217;, <br \/> &#8216;desc&#8217; => &#8216;Low voltage washer&#8217;, <br \/> &#8216;price&#8217; => &#8216;12.00&#8217; <br \/> } <br \/> ] <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Then you could access the description of the first part in the catalog with this code:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">print $catalog\u2212>{part}\u2212>[0]\u2212>{desc}, &#8220;n&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">However, if you wanted to access the description of the part with the part number of &#8220;9344675&#8221; then you\u2019d have to code a loop like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">foreach my $part (@{$catalog\u2212>{part}}) { <br \/> if($part\u2212>{partnum} eq &#8216;9344675&#8217;) { <br \/> print $part\u2212>{desc}, &#8220;n&#8221;; <br \/> last; <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The knowledge that each <part> element has a unique partnum attribute allows you to eliminate this search. You can pass this knowledge on to XML::Simple like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $catalog = XMLin($xml, keyattr => [&#8216;partnum&#8217;]);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Which will return a data structure like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$catalog = { <br \/> &#8216;part&#8217; => { <br \/> &#8216;5634896&#8217; => { &#8216;desc&#8217; => &#8216;Low voltage washer&#8217;, &#8216;price&#8217; => &#8216;12.00&#8217; }, <br \/> &#8216;1842334&#8217; => { &#8216;desc&#8217; => &#8216;High pressure flange&#8217;, &#8216;price&#8217; => &#8216;24.50&#8217; }, <br \/> &#8216;9344675&#8217; => { &#8216;desc&#8217; => &#8216;Threaded gasket&#8217;, &#8216;price&#8217; => &#8216;9.25&#8217; } <br \/> } <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">XML::Simple has been able to transform $catalog\u2212>{part} from an arrayref to a hashref (keyed on partnum). This transformation is called \u2019array folding\u2019.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Through the use of array folding, you can now index directly to the description of the part you want:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">print $catalog\u2212>{part}\u2212>{9344675}\u2212>{desc}, &#8220;n&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The \u2019keyattr\u2019 option also enables array folding when the unique key is in a nested element rather than an attribute. eg:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><catalog>  <part> <partnum>1842334<\/partnum> <desc>High pressure flange<\/desc>  <price>24.50<\/price> <\/part> <part> <partnum>9344675<\/partnum> <desc>Threaded gasket<\/desc>  <price>9.25<\/price> <\/part> <part> <partnum>5634896<\/partnum> <desc>Low voltage washer<\/desc>  <price>12.00<\/price> <\/part> <\/catalog><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">See the XML::Simple manual page for more information.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>So what\u2019s the catch with \u2019keyattr\u2019?<\/b> <br \/> One thing to watch out for is that you might get array folding even if you don\u2019t supply the keyattr option. The default value for this option is:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">[ &#8216;name&#8217;, &#8216;key&#8217;, &#8216;id&#8217;]<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Which means if your <small>XML<\/small> elements have a \u2019name\u2019, \u2019key\u2019 or \u2019id\u2019 attribute (or nested element) then they may get folded on those values. This means that you can take advantage of array folding simply through careful choice of attribute names. On the hand, if you really don\u2019t want array folding at all, you\u2019ll need to set \u2019key attr to an empty list:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $ref = XMLin($xml, keyattr => []);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A second \u2019gotcha\u2019 is that array folding only works on arrays. That might seem obvious, but if there\u2019s only one record in your <small>XML<\/small> and you didn\u2019t set the \u2019forcearray\u2019 option then it won\u2019t be represented as an array and consequently won\u2019t get folded into a hash. The moral is that if you\u2019re using array folding, you should always turn on the forcearray option.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You probably want to be as specific as you can be too. For instance, the safest way to parse the <catalog> example above would be:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $catalog = XMLin($xml, keyattr => { part => &#8216;partnum&#8217;}, <br \/> forcearray => [&#8216;part&#8217;]);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">By using the hashref for keyattr, you can specify that only <part> elements should be folded on the \u2019partnum\u2019 attribute (and that the <part> elements should not be folded on any other attribute).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">By supplying a list of element names for forcearray, you\u2019re ensuring that folding will work even if there\u2019s only one <part>. You\u2019re also ensuring that if the \u2019partnum\u2019 unique key is supplied in a nested element then that element won\u2019t get forced to an array too.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>How do I know what my data structure should look like?<\/b> <br \/> The rules are fairly straightforward:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">each element gets represented as a hash<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>unless it contains only text, in which case it\u2019ll be a simple scalar value<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>or unless there\u2019s more than one element with the same name, in which case they\u2019ll be represented as an array<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>unless you\u2019ve got array folding enabled, in which case they\u2019ll be folded into a hash<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>empty elements (no text contents <b>and<\/b> no attributes) will either be represented as an empty hash, an empty string or undef \u2212 depending on the value of the \u2019suppressempty\u2019 option.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">If you\u2019re in any doubt, use Data::Dumper, eg:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use XML::Simple; <br \/> use Data::Dumper; <br \/> my $ref = XMLin($xml); <br \/> print Dumper($ref);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>I\u2019m getting \u2019Use of uninitialized value\u2019 warnings<\/b> <br \/> You\u2019re probably trying to index into a non-existant hash key \u2212 try Data::Dumper.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>I\u2019m getting a \u2019Not an <small>ARRAY<\/small> reference\u2019 error<\/b> <br \/> Something that you expect to be an array is not. The two most likely causes are that you forgot to use \u2019forcearray\u2019 or that the array got folded into a hash \u2212 try Data::Dumper.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>I\u2019m getting a \u2019No such array field\u2019 error<\/b> <br \/> Something that you expect to be a hash is actually an array. Perhaps array folding failed because one element was missing the key attribute \u2212 try Data::Dumper.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>I\u2019m getting an \u2019Out of memory\u2019 error<\/b> <br \/> Something in the data structure is not as you expect and Perl may be trying unsuccessfully to autovivify things \u2212 try Data::Dumper.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you\u2019re already using Data::Dumper, try calling <b>Dumper()<\/b> immediately after <b>XMLin()<\/b> \u2212 ie: before you attempt to access anything in the data structure.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>My element order is getting jumbled up<\/b> <br \/> If you read an <small>XML<\/small> file with <b>XMLin()<\/b> and then write it back out with <b>XMLout()<\/b>, the order of the elements will likely be different. (However, if you read the file back in with <b>XMLin()<\/b> you\u2019ll get the same Perl data structure).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The reordering happens because XML::Simple uses hashrefs to store your data and Perl hashes do not really have any order.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">It is possible that a future version of XML::Simple will use Tie::IxHash to store the data in hashrefs which do retain the order. However this will not fix all cases of element order being lost.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If your application really is sensitive to element order, don\u2019t use XML::Simple (and don\u2019t put order-sensitive values in attributes).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>XML::Simple turns nested elements into attributes<\/b> <br \/> If you read an <small>XML<\/small> file with <b>XMLin()<\/b> and then write it back out with <b>XMLout()<\/b>, some data which was originally stored in nested elements may end up in attributes. (However, if you read the file back in with <b>XMLin()<\/b> you\u2019ll get the same Perl data structure).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">There are a number of ways you might handle this:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"61%\">\n<p style=\"margin-top: 1em\">use the \u2019forcearray\u2019 option with <b>XMLin()<\/b><\/p>\n<\/td>\n<td width=\"22%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"61%\">\n<p>use the \u2019noattr\u2019 option with <b>XMLout()<\/b><\/p>\n<\/td>\n<td width=\"22%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"61%\">\n<p>live with it<\/p>\n<\/td>\n<td width=\"22%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"61%\">\n<p>don\u2019t use XML::Simple<\/p>\n<\/td>\n<td width=\"22%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Why does XMLout() insert <name> elements (or attributes)?<\/b> <br \/> Try setting keyattr => [].<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When you call <b>XMLin()<\/b> to read <small>XML,<\/small> the \u2019keyattr\u2019 option controls whether arrays get \u2019folded\u2019 into hashes. Similarly, when you call <b>XMLout()<\/b>, the \u2019keyattr\u2019 option controls whether hashes get \u2019unfolded\u2019 into arrays. As described above, \u2019keyattr\u2019 is enabled by default.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Why are empty elements represented as empty hashes?<\/b> <br \/> An element is always represented as a hash unless it contains only text, in which case it is represented as a scalar string.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you would prefer empty elements to be represented as empty strings or the undefined value, set the \u2019suppressempty\u2019 option to \u2019\u2019 or undef respectively.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Why is ParserOpts deprecated?<\/b> <br \/> The &#8220;ParserOpts&#8221; option is a remnant of the time when XML::Simple only worked with the XML::Parser <small>API.<\/small> Its value is completely ignored if you\u2019re using a <small>SAX<\/small> parser, so writing code which relied on it would bar you from taking advantage of <small>SAX.<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Even if you are using XML::Parser, it is seldom necessary to pass options to the parser object. A number of people have written to say they use this option to set XML::Parser\u2019s &#8220;ProtocolEncoding&#8221; option. Don\u2019t do that, it\u2019s wrong, Wrong, <small>WRONG<\/small> ! Fix the <small>XML<\/small> document so that it\u2019s well-formed and you won\u2019t have a problem.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Having said all of that, as long as XML::Simple continues to support the XML::Parser <small>API,<\/small> this option will not be removed. There are currently no plans to remove support for the XML::Parser <small>API.<\/small><\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  XML::Simple::FAQ \u2212 Frequently Asked Questions about XML::Simple <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3007,3524],"class_list":["post-7193","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-man3","tag-xmlsimplefaq"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=7193"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7193\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}