{"id":6981,"date":"2022-12-20T19:34:34","date_gmt":"2022-12-20T22:34:34","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/xmlsimple-man3\/"},"modified":"2022-12-20T19:34:34","modified_gmt":"2022-12-20T22:34:34","slug":"xmlsimple-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/xmlsimple-man3\/","title":{"rendered":"XML::Simple (man3)"},"content":{"rendered":"<h1 align=\"center\">XML::Simple<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#STATUS OF THIS MODULE\">STATUS OF THIS MODULE<\/a><br \/> <a href=\"#QUICK START\">QUICK START<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#OPTIONS\">OPTIONS<\/a><br \/> <a href=\"#OPTIONAL OO INTERFACE\">OPTIONAL OO INTERFACE<\/a><br \/> <a href=\"#STRICT MODE\">STRICT MODE<\/a><br \/> <a href=\"#SAX SUPPORT\">SAX SUPPORT<\/a><br \/> <a href=\"#ENVIRONMENT\">ENVIRONMENT<\/a><br \/> <a href=\"#ERROR HANDLING\">ERROR HANDLING<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#WHERE TO FROM HERE?\">WHERE TO FROM HERE?<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">XML::Simple \u2212 An API for simple XML files<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><small>PLEASE DO NOT USE THIS MODULE IN NEW CODE.<\/small> If you ignore this warning and use it anyway, the &#8220;qw(:strict)&#8221; mode will save you a little pain.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use XML::Simple qw(:strict); <br \/> my $ref = XMLin([<xml file or string>] [,<options>]); <br \/> my $xml = XMLout($hashref [,<options>]);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Or the object oriented way:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">require XML::Simple qw(:strict); <br \/> my $xs = XML::Simple\u2212>new([<options>]); <br \/> my $ref = $xs\u2212>XMLin([<xml file or string>] [,<options>]); <br \/> my $xml = $xs\u2212>XMLout($hashref [,<options>]);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">(or see &#8221; <small>SAX SUPPORT&#8221;<\/small> for \u2019the <small>SAX<\/small> way\u2019).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note, in these examples, the square brackets are used to denote optional items not to imply items should be supplied in arrayrefs.<\/p>\n<h2>STATUS OF THIS MODULE <a name=\"STATUS OF THIS MODULE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The use of this module in new code is <b>strongly discouraged<\/b>. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended and you can refer to Perl XML::LibXML by Example <http:\/\/grantm.github.io\/perl-libxml-by-example\/> for a tutorial introduction.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">XML::Twig is another excellent alternative.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The major problems with this module are the large number of options (some of which have unfortunate defaults) and the arbitrary ways in which these options interact \u2212 often producing unexpected results.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Patches with bug fixes and documentation fixes are welcome, but new features are unlikely to be added.<\/p>\n<h2>QUICK START <a name=\"QUICK START\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Say you have a script called <b>foo<\/b> and a file of configuration options called <b>foo.xml<\/b> containing the following:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><config logdir=\"\/var\/log\/foo\/\" debugfile=\"\/tmp\/foo.debug\"> <br \/> <server name=\"sahara\" osname=\"solaris\" osversion=\"2.6\">  <\/p>\n<address>10.0.0.101<\/address>\n<p> <\/p>\n<address>10.0.1.101<\/address>\n<p> <\/server> <br \/> <server name=\"gobi\" osname=\"irix\" osversion=\"6.5\">  <\/p>\n<address>10.0.0.102<\/address>\n<p> <\/server> <br \/> <server name=\"kalahari\" osname=\"linux\" osversion=\"2.0.34\">  <\/p>\n<address>10.0.0.103<\/address>\n<p> <\/p>\n<address>10.0.1.103<\/address>\n<p> <\/server> <br \/> <\/config><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The following lines of code in <b>foo<\/b>:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use XML::Simple qw(:strict); <br \/> my $config = XMLin(undef, KeyAttr => { server => &#8216;name&#8217; }, ForceArray => [ &#8216;server&#8217;, &#8216;address&#8217; ]);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">will \u2019slurp\u2019 the configuration options into the hashref $config (because no filename or <small>XML<\/small> string was passed as the first argument to &#8220;XMLin()&#8221; the name and location of the <small>XML<\/small> file will be inferred from name and location of the script). You can dump out the contents of the hashref using Data::Dumper:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use Data::Dumper; <br \/> print Dumper($config);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">which will produce something like this (formatting has been adjusted for brevity):<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> &#8216;logdir&#8217; => &#8216;\/var\/log\/foo\/&#8217;, <br \/> &#8216;debugfile&#8217; => &#8216;\/tmp\/foo.debug&#8217;, <br \/> &#8216;server&#8217; => { <br \/> &#8216;sahara&#8217; => { <br \/> &#8216;osversion&#8217; => &#8216;2.6&#8217;, <br \/> &#8216;osname&#8217; => &#8216;solaris&#8217;, <br \/> &#8216;address&#8217; => [ &#8216;10.0.0.101&#8217;, &#8216;10.0.1.101&#8217; ] <br \/> }, <br \/> &#8216;gobi&#8217; => { <br \/> &#8216;osversion&#8217; => &#8216;6.5&#8217;, <br \/> &#8216;osname&#8217; => &#8216;irix&#8217;, <br \/> &#8216;address&#8217; => [ &#8216;10.0.0.102&#8217; ] <br \/> }, <br \/> &#8216;kalahari&#8217; => { <br \/> &#8216;osversion&#8217; => &#8216;2.0.34&#8217;, <br \/> &#8216;osname&#8217; => &#8216;linux&#8217;, <br \/> &#8216;address&#8217; => [ &#8216;10.0.0.103&#8217;, &#8216;10.0.1.103&#8217; ] <br \/> } <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Your script could then access the name of the log directory like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">print $config\u2212>{logdir};<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">similarly, the second address on the server \u2019kalahari\u2019 could be referenced as:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">print $config\u2212>{server}\u2212>{kalahari}\u2212>{address}\u2212>[1];<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note: If the mapping between the output of Data::Dumper and the print statements above is not obvious to you, then please refer to the \u2019references\u2019 tutorial ( <small>AKA:<\/small> &#8220;Mark\u2019s very short tutorial about references&#8221;) at perlreftut.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In this example, the &#8220;ForceArray&#8221; option was used to list elements that might occur multiple times and should therefore be represented as arrayrefs (even when only one element is present).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;KeyAttr&#8221; option was used to indicate that each &#8220;<server>&#8221; element has a unique identifier in the &#8220;name&#8221; attribute. This allows you to index directly to a particular server record using the name as a hash key (as shown above).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For simple requirements, that\u2019s really all there is to it. If you want to store your <small>XML<\/small> in a different directory or file, or pass it in as a string or even pass it in via some derivative of an IO::Handle, you\u2019ll need to check out &#8221; <small>OPTIONS&#8221;<\/small> . If you want to turn off or tweak the array folding feature (that neat little transformation that produced $config\u2212>{server}) you\u2019ll find options for that as well.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you want to generate <small>XML<\/small> (for example to write a modified version of $config back out as <small>XML<\/small> ), check out &#8220;XMLout()&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If your needs are not so simple, this may not be the module for you. In that case, you might want to read &#8221; <small>WHERE TO FROM HERE<\/small> ?&#8221;.<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The XML::Simple module provides a simple <small>API<\/small> layer on top of an underlying <small>XML<\/small> parsing module (either XML::Parser or one of the <small>SAX2<\/small> parser modules). Two functions are exported: &#8220;XMLin()&#8221; and &#8220;XMLout()&#8221;. Note: you can explicitly request the lower case versions of the function names: &#8220;xml_in()&#8221; and &#8220;xml_out()&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The simplest approach is to call these two functions directly, but an optional object oriented interface (see &#8221; <small>OPTIONAL OO INTERFACE&#8221;<\/small> below) allows them to be called as methods of an <b>XML::Simple<\/b> object. The object interface can also be used at either end of a <small>SAX<\/small> pipeline.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>XMLin()<\/b> <br \/> Parses <small>XML<\/small> formatted data and returns a reference to a data structure which contains the same information in a more readily accessible form. (Skip down to &#8221; <small>EXAMPLES&#8221;<\/small> below, for more sample code).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">&#8220;XMLin()&#8221; accepts an optional <small>XML<\/small> specifier followed by zero or more \u2019name => value\u2019 option pairs. The <small>XML<\/small> specifier can be one of the following: <br \/> A filename<\/p>\n<p style=\"margin-left:17%;\">If the filename contains no directory components &#8220;XMLin()&#8221; will look for the file in each directory in the SearchPath (see &#8221; <small>OPTIONS&#8221;<\/small> below) or in the current directory if the SearchPath option is not defined. eg:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$ref = XMLin(&#8216;\/etc\/params.xml&#8217;);<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">Note, the filename \u2019\u2212\u2019 can be used to parse from <small>STDIN.<\/small><\/p>\n<p style=\"margin-left:11%;\">undef<\/p>\n<p style=\"margin-left:17%;\">If there is no <small>XML<\/small> specifier, &#8220;XMLin()&#8221; will check the script directory and each of the SearchPath directories for a file with the same name as the script but with the extension \u2019.xml\u2019. Note: if you wish to specify options, you must specify the value \u2019undef\u2019. eg:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$ref = XMLin(undef, ForceArray => 1);<\/p>\n<p style=\"margin-left:11%;\">A string of <small>XML<\/small><\/p>\n<p style=\"margin-left:17%;\">A string containing <small>XML<\/small> (recognised by the presence of \u2019<\u2019 and \u2019>\u2019 characters) will be parsed directly. eg:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$ref = XMLin(&#8216;<opt username=\"bob\" password=\"flurp\" \/>&#8216;);<\/p>\n<p style=\"margin-left:11%;\">An IO::Handle object<\/p>\n<p style=\"margin-left:17%;\">An IO::Handle object will be read to <small>EOF<\/small> and its contents parsed. eg:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$fh = IO::File\u2212>new(&#8216;\/etc\/params.xml&#8217;); <br \/> $ref = XMLin($fh);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>XMLout()<\/b> <br \/> Takes a data structure (generally a hashref) and returns an <small>XML<\/small> encoding of that structure. If the resulting <small>XML<\/small> is parsed using &#8220;XMLin()&#8221;, it should return a data structure equivalent to the original (see caveats below).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;XMLout()&#8221; function can also be used to output the <small>XML<\/small> as <small>SAX<\/small> events see the &#8220;Handler&#8221; option and &#8221; <small>SAX SUPPORT&#8221;<\/small> for more details).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When translating hashes to <small>XML,<\/small> hash keys which have a leading \u2019\u2212\u2019 will be silently skipped. This is the approved method for marking elements of a data structure which should be ignored by &#8220;XMLout&#8221;. (Note: If these items were not skipped the key names would be emitted as element or attribute names with a leading \u2019\u2212\u2019 which would not be valid <small>XML<\/small> ).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Caveats<\/b> <br \/> Some care is required in creating data structures which will be passed to &#8220;XMLout()&#8221;. Hash keys from the data structure will be encoded as either <small>XML<\/small> element names or attribute names. Therefore, you should use hash key names which conform to the relatively strict <small>XML<\/small> naming rules:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Names in <small>XML<\/small> must begin with a letter. The remaining characters may be letters, digits, hyphens (\u2212), underscores (_) or full stops (.). It is also allowable to include one colon (:) in an element name but this should only be used when working with namespaces (<b>XML::Simple<\/b> can only usefully work with namespaces when teamed with a <small>SAX<\/small> Parser).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can use other punctuation characters in hash values (just not in hash keys) however <b>XML::Simple<\/b> does not support dumping binary data.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you break these rules, the current implementation of &#8220;XMLout()&#8221; will simply emit non-compliant <small>XML<\/small> which will be rejected if you try to read it back in. (A later version of <b>XML::Simple<\/b> might take a more proactive approach).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note also that although you can nest hashes and arrays to arbitrary levels, circular data structures are not supported and will cause &#8220;XMLout()&#8221; to die.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you wish to \u2019round\u2212trip\u2019 arbitrary data structures from Perl to <small>XML<\/small> and back to Perl, then you should probably disable array folding (using the KeyAttr option) both with &#8220;XMLout()&#8221; and with &#8220;XMLin()&#8221;. If you still don\u2019t get the expected results, you may prefer to use XML::Dumper which is designed for exactly that purpose.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Refer to &#8221; <small>WHERE TO FROM HERE<\/small> ?&#8221; if &#8220;XMLout()&#8221; is too simple for your needs.<\/p>\n<h2>OPTIONS <a name=\"OPTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>XML::Simple<\/b> supports a number of options (in fact as each release of <b>XML::Simple<\/b> adds more options, the module\u2019s claim to the name \u2019Simple\u2019 becomes increasingly tenuous). If you find yourself repeatedly having to specify the same options, you might like to investigate &#8221; <small>OPTIONAL OO INTERFACE&#8221;<\/small> below.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you can\u2019t be bothered reading the documentation, refer to &#8221; <small>STRICT MODE&#8221;<\/small> to automatically catch common mistakes.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Because there are so many options, it\u2019s hard for new users to know which ones are important, so here are the two you really need to know about:<\/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>check out &#8220;ForceArray&#8221; because you\u2019ll almost certainly want to turn it on<\/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>make sure you know what the &#8220;KeyAttr&#8221; option does and what its default value is because it may surprise you otherwise (note in particular that \u2019KeyAttr\u2019 affects both &#8220;XMLin&#8221; and &#8220;XMLout&#8221;)<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">The option name headings below have a trailing \u2019comment\u2019 \u2212 a hash followed by two pieces of metadata:<\/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\">Options are marked with \u2019<i>in<\/i>\u2019 if they are recognised by &#8220;XMLin()&#8221; and \u2019<i>out<\/i>\u2019 if they are recognised by &#8220;XMLout()&#8221;.<\/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>Each option is also flagged to indicate whether it is:<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">&#8216;important&#8217; \u2212 don&#8217;t use the module until you understand this one <br \/> &#8216;handy&#8217; \u2212 you can skip this on the first time through <br \/> &#8216;advanced&#8217; \u2212 you can skip this on the second time through <br \/> &#8216;SAX only&#8217; \u2212 don&#8217;t worry about this unless you&#8217;re using SAX (or <br \/> alternatively if you need this, you also need SAX) <br \/> &#8216;seldom used&#8217; \u2212 you&#8217;ll probably never use this unless you were the <br \/> person that requested the feature<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The options are listed alphabetically:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note: option names are no longer case sensitive so you can use the mixed case versions shown here; all lower case as required by versions 2.03 and earlier; or you can add underscores between the words (eg: key_attr).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>AttrIndent => 1<\/b> <i># out \u2212 handy<\/i> <br \/> When you are using &#8220;XMLout()&#8221;, enable this option to have attributes printed one-per-line with sensible indentation rather than all on one line.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Cache => [ cache schemes ]<\/b> <i># in \u2212 advanced<\/i> <br \/> Because loading the <b>XML::Parser<\/b> module and parsing an <small>XML<\/small> file can consume a significant number of <small>CPU<\/small> cycles, it is often desirable to cache the output of &#8220;XMLin()&#8221; for later reuse.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When parsing from a named file, <b>XML::Simple<\/b> supports a number of caching schemes. The \u2019Cache\u2019 option may be used to specify one or more schemes (using an anonymous array). Each scheme will be tried in turn in the hope of finding a cached pre-parsed representation of the <small>XML<\/small> file. If no cached copy is found, the file will be parsed and the first cache scheme in the list will be used to save a copy of the results. The following cache schemes have been implemented: <br \/> storable<\/p>\n<p style=\"margin-left:17%;\">Utilises <b>Storable.pm<\/b> to read\/write a cache file with the same name as the <small>XML<\/small> file but with the extension .stor<\/p>\n<p style=\"margin-left:11%;\">memshare<\/p>\n<p style=\"margin-left:17%;\">When a file is first parsed, a copy of the resulting data structure is retained in memory in the <b>XML::Simple<\/b> module\u2019s namespace. Subsequent calls to parse the same file will return a reference to this structure. This cached version will persist only for the life of the Perl interpreter (which in the case of mod_perl for example, may be some significant time).<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">Because each caller receives a reference to the same data structure, a change made by one caller will be visible to all. For this reason, the reference returned should be treated as read-only.<\/p>\n<p style=\"margin-left:11%;\">memcopy<\/p>\n<p style=\"margin-left:17%;\">This scheme works identically to \u2019memshare\u2019 (above) except that each caller receives a reference to a new data structure which is a copy of the cached version. Copying the data structure will add a little processing overhead, therefore this scheme should only be used where the caller intends to modify the data structure (or wishes to protect itself from others who might). This scheme uses <b>Storable.pm<\/b> to perform the copy.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Warning! The memory-based caching schemes compare the timestamp on the file to the time when it was last parsed. If the file is stored on an <small>NFS<\/small> filesystem (or other network share) and the clock on the file server is not exactly synchronised with the clock where your script is run, updates to the source <small>XML<\/small> file may appear to be ignored.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>ContentKey => \u2019keyname\u2019<\/b> <i># in+out \u2212 seldom used<\/i> <br \/> When text content is parsed to a hash value, this option lets you specify a name for the hash key to override the default \u2019content\u2019. So for example:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">XMLin(&#8216;<opt one=\"1\">Text<\/opt>&#8216;, ContentKey => &#8216;text&#8217;)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">will parse to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ &#8216;one&#8217; => 1, &#8216;text&#8217; => &#8216;Text&#8217; }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">instead of:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ &#8216;one&#8217; => 1, &#8216;content&#8217; => &#8216;Text&#8217; }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">&#8220;XMLout()&#8221; will also honour the value of this option when converting a hashref to <small>XML.<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can also prefix your selected key name with a \u2019\u2212\u2019 character to have &#8220;XMLin()&#8221; try a little harder to eliminate unnecessary \u2019content\u2019 keys after array folding. For example:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">XMLin( <br \/> &#8216;<opt><item name=\"one\">First<\/item><item name=\"two\">Second<\/item><\/opt>&#8216;, <br \/> KeyAttr => {item => &#8216;name&#8217;}, <br \/> ForceArray => [ &#8216;item&#8217; ], <br \/> ContentKey => &#8216;\u2212content&#8217; <br \/> )<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">will parse to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> &#8216;item&#8217; => { <br \/> &#8216;one&#8217; => &#8216;First&#8217; <br \/> &#8216;two&#8217; => &#8216;Second&#8217; <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">rather than this (without the \u2019\u2212\u2019):<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> &#8216;item&#8217; => { <br \/> &#8216;one&#8217; => { &#8216;content&#8217; => &#8216;First&#8217; } <br \/> &#8216;two&#8217; => { &#8216;content&#8217; => &#8216;Second&#8217; } <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>DataHandler => code_ref<\/b> <i># in \u2212 <small>SAX<\/small> only<\/i> <br \/> When you use an <b>XML::Simple<\/b> object as a <small>SAX<\/small> handler, it will return a \u2019simple tree\u2019 data structure in the same format as &#8220;XMLin()&#8221; would return. If this option is set (to a subroutine reference), then when the tree is built the subroutine will be called and passed two arguments: a reference to the <b>XML::Simple<\/b> object and a reference to the data tree. The return value from the subroutine will be returned to the <small>SAX<\/small> driver. (See &#8221; <small>SAX SUPPORT&#8221;<\/small> for more details).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>ForceArray => 1<\/b> <i># in \u2212 important<\/i> <br \/> This option should be set to \u20191\u2019 to force nested elements to be represented as arrays even when there is only one. Eg, with ForceArray enabled, this <small>XML:<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><opt> <br \/> <name>value<\/name> <br \/> <\/opt><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">would parse to this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> &#8216;name&#8217; => [ <br \/> &#8216;value&#8217; <br \/> ] <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">instead of this (the default):<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> &#8216;name&#8217; => &#8216;value&#8217; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This option is especially useful if the data structure is likely to be written back out as <small>XML<\/small> and the default behaviour of rolling single nested elements up into attributes is not desirable.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you are using the array folding feature, you should almost certainly enable this option. If you do not, single nested elements will not be parsed to arrays and therefore will not be candidates for folding to a hash. (Given that the default value of \u2019KeyAttr\u2019 enables array folding, the default value of this option should probably also have been enabled too \u2212 sorry).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>ForceArray => [ names ]<\/b> <i># in \u2212 important<\/i> <br \/> This alternative (and preferred) form of the \u2019ForceArray\u2019 option allows you to specify a list of element names which should always be forced into an array representation, rather than the \u2019all or nothing\u2019 approach above.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">It is also possible (since version 2.05) to include compiled regular expressions in the list \u2212 any element names which match the pattern will be forced to arrays. If the list contains only a single regex, then it is not necessary to enclose it in an arrayref. Eg:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">ForceArray => qr\/_list$\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>ForceContent => 1<\/b> <i># in \u2212 seldom used<\/i> <br \/> When &#8220;XMLin()&#8221; parses elements which have text content as well as attributes, the text content must be represented as a hash value rather than a simple scalar. This option allows you to force text content to always parse to a hash value even when there are no attributes. So for example:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">XMLin(&#8216;<opt><x>text1<\/x><y a=\"2\">text2<\/y><\/opt>&#8216;, ForceContent => 1)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">will parse to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> &#8216;x&#8217; => { &#8216;content&#8217; => &#8216;text1&#8217; }, <br \/> &#8216;y&#8217; => { &#8216;a&#8217; => 2, &#8216;content&#8217; => &#8216;text2&#8217; } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">instead of:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> &#8216;x&#8217; => &#8216;text1&#8217;, <br \/> &#8216;y&#8217; => { &#8216;a&#8217; => 2, &#8216;content&#8217; => &#8216;text2&#8217; } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>GroupTags => { grouping tag => grouped tag }<\/b> <i># in+out \u2212 handy<\/i> <br \/> You can use this option to eliminate extra levels of indirection in your Perl data structure. For example this <small>XML:<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><opt> <br \/> <searchpath> <br \/> <dir>\/usr\/bin<\/dir> <br \/> <dir>\/usr\/local\/bin<\/dir> <br \/> <dir>\/usr\/X11\/bin<\/dir> <br \/> <\/searchpath> <br \/> <\/opt><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Would normally be read into a structure like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> searchpath => { <br \/> dir => [ &#8216;\/usr\/bin&#8217;, &#8216;\/usr\/local\/bin&#8217;, &#8216;\/usr\/X11\/bin&#8217; ] <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">But when read in with the appropriate value for \u2019GroupTags\u2019:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $opt = XMLin($xml, GroupTags => { searchpath => &#8216;dir&#8217; });<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">It will return this simpler structure:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> searchpath => [ &#8216;\/usr\/bin&#8217;, &#8216;\/usr\/local\/bin&#8217;, &#8216;\/usr\/X11\/bin&#8217; ] <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The grouping element (&#8220;<searchpath>&#8221; in the example) must not contain any attributes or elements other than the grouped element.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can specify multiple \u2019grouping element\u2019 to \u2019grouped element\u2019 mappings in the same hashref. If this option is combined with &#8220;KeyAttr&#8221;, the array folding will occur first and then the grouped element names will be eliminated.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">&#8220;XMLout&#8221; will also use the grouptag mappings to re-introduce the tags around the grouped elements. Beware though that this will occur in all places that the \u2019grouping tag\u2019 name occurs \u2212 you probably don\u2019t want to use the same name for elements as well as attributes.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Handler => object_ref<\/b> <i># out \u2212 <small>SAX<\/small> only<\/i> <br \/> Use the \u2019Handler\u2019 option to have &#8220;XMLout()&#8221; generate <small>SAX<\/small> events rather than returning a string of <small>XML.<\/small> For more details see &#8221; <small>SAX SUPPORT&#8221;<\/small> below.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note: the current implementation of this option generates a string of <small>XML<\/small> and uses a <small>SAX<\/small> parser to translate it into <small>SAX<\/small> events. The normal encoding rules apply here \u2212 your data must be <small>UTF8<\/small> encoded unless you specify an alternative encoding via the \u2019XMLDecl\u2019 option; and by the time the data reaches the handler object, it will be in <small>UTF8<\/small> form regardless of the encoding you supply. A future implementation of this option may generate the events directly.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>KeepRoot => 1<\/b> <i># in+out \u2212 handy<\/i> <br \/> In its attempt to return a data structure free of superfluous detail and unnecessary levels of indirection, &#8220;XMLin()&#8221; normally discards the root element name. Setting the \u2019KeepRoot\u2019 option to \u20191\u2019 will cause the root element name to be retained. So after executing this code:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$config = XMLin(&#8216;<config tempdir=\"\/tmp\" \/>&#8216;, KeepRoot => 1)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You\u2019ll be able to reference the tempdir as &#8220;$config\u2212>{config}\u2212>{tempdir}&#8221; instead of the default &#8220;$config\u2212>{tempdir}&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Similarly, setting the \u2019KeepRoot\u2019 option to \u20191\u2019 will tell &#8220;XMLout()&#8221; that the data structure already contains a root element name and it is not necessary to add another.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>KeyAttr => [ list ]<\/b> <i># in+out \u2212 important<\/i> <br \/> This option controls the \u2019array folding\u2019 feature which translates nested elements from an array to a hash. It also controls the \u2019unfolding\u2019 of hashes to arrays.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For example, this <small>XML:<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><opt> <br \/> <user login=\"grep\" fullname=\"Gary R Epstein\" \/> <br \/> <user login=\"stty\" fullname=\"Simon T Tyson\" \/> <br \/> <\/opt><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">would, by default, parse to this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> &#8216;user&#8217; => [ <br \/> { <br \/> &#8216;login&#8217; => &#8216;grep&#8217;, <br \/> &#8216;fullname&#8217; => &#8216;Gary R Epstein&#8217; <br \/> }, <br \/> { <br \/> &#8216;login&#8217; => &#8216;stty&#8217;, <br \/> &#8216;fullname&#8217; => &#8216;Simon T Tyson&#8217; <br \/> } <br \/> ] <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the option \u2019KeyAttr => &#8220;login&#8221;\u2019 were used to specify that the \u2019login\u2019 attribute is a key, the same <small>XML<\/small> would parse to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> &#8216;user&#8217; => { <br \/> &#8216;stty&#8217; => { <br \/> &#8216;fullname&#8217; => &#8216;Simon T Tyson&#8217; <br \/> }, <br \/> &#8216;grep&#8217; => { <br \/> &#8216;fullname&#8217; => &#8216;Gary R Epstein&#8217; <br \/> } <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The key attribute names should be supplied in an arrayref if there is more than one. &#8220;XMLin()&#8221; will attempt to match attribute names in the order supplied. &#8220;XMLout()&#8221; will use the first attribute name supplied when \u2019unfolding\u2019 a hash into an array.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note 1: The default value for \u2019KeyAttr\u2019 is [\u2019name\u2019, \u2019key\u2019, \u2019id\u2019]. If you do not want folding on input or unfolding on output you must set this option to an empty list to disable the feature.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note 2: If you wish to use this option, you should also enable the &#8220;ForceArray&#8221; option. Without \u2019ForceArray\u2019, a single nested element will be rolled up into a scalar rather than an array and therefore will not be folded (since only arrays get folded).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>KeyAttr => { list }<\/b> <i># in+out \u2212 important<\/i> <br \/> This alternative (and preferred) method of specifying the key attributes allows more fine grained control over which elements are folded and on which attributes. For example the option \u2019KeyAttr => { package => \u2019id\u2019 } will cause any package elements to be folded on the \u2019id\u2019 attribute. No other elements which have an \u2019id\u2019 attribute will be folded at all.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note: &#8220;XMLin()&#8221; will generate a warning (or a fatal error in &#8221; <small>STRICT MODE&#8221;<\/small> ) if this syntax is used and an element which does not have the specified key attribute is encountered (eg: a \u2019package\u2019 element without an \u2019id\u2019 attribute, to use the example above). Warnings can be suppressed with the lexical &#8220;no warnings;&#8221; pragma or &#8220;no warnings &#8216;XML::Simple&#8217;;&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Two further variations are made possible by prefixing a \u2019+\u2019 or a \u2019\u2212\u2019 character to the attribute name:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The option \u2019KeyAttr => { user => &#8220;+login&#8221; }\u2019 will cause this <small>XML:<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><opt> <br \/> <user login=\"grep\" fullname=\"Gary R Epstein\" \/> <br \/> <user login=\"stty\" fullname=\"Simon T Tyson\" \/> <br \/> <\/opt><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">to parse to this data structure:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> &#8216;user&#8217; => { <br \/> &#8216;stty&#8217; => { <br \/> &#8216;fullname&#8217; => &#8216;Simon T Tyson&#8217;, <br \/> &#8216;login&#8217; => &#8216;stty&#8217; <br \/> }, <br \/> &#8216;grep&#8217; => { <br \/> &#8216;fullname&#8217; => &#8216;Gary R Epstein&#8217;, <br \/> &#8216;login&#8217; => &#8216;grep&#8217; <br \/> } <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The \u2019+\u2019 indicates that the value of the key attribute should be copied rather than moved to the folded hash key.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A \u2019\u2212\u2019 prefix would produce this result:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> &#8216;user&#8217; => { <br \/> &#8216;stty&#8217; => { <br \/> &#8216;fullname&#8217; => &#8216;Simon T Tyson&#8217;, <br \/> &#8216;\u2212login&#8217; => &#8216;stty&#8217; <br \/> }, <br \/> &#8216;grep&#8217; => { <br \/> &#8216;fullname&#8217; => &#8216;Gary R Epstein&#8217;, <br \/> &#8216;\u2212login&#8217; => &#8216;grep&#8217; <br \/> } <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">As described earlier, &#8220;XMLout&#8221; will ignore hash keys starting with a \u2019\u2212\u2019.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>NoAttr => 1<\/b> <i># in+out \u2212 handy<\/i> <br \/> When used with &#8220;XMLout()&#8221;, the generated <small>XML<\/small> will contain no attributes. All hash key\/values will be represented as nested elements instead.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When used with &#8220;XMLin()&#8221;, any attributes in the <small>XML<\/small> will be ignored.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>NoEscape => 1<\/b> <i># out \u2212 seldom used<\/i> <br \/> By default, &#8220;XMLout()&#8221; will translate the characters \u2019<\u2019, \u2019>\u2019, \u2019&#038;\u2019 and \u2019&#8221;\u2019 to \u2019&lt;\u2019, \u2019&gt;\u2019, \u2019&amp;\u2019 and \u2019&#038;quot\u2019 respectively. Use this option to suppress escaping (presumably because you\u2019ve already escaped the data in some more sophisticated manner).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>NoIndent => 1<\/b> <i># out \u2212 seldom used<\/i> <br \/> Set this option to 1 to disable &#8220;XMLout()&#8221;\u2019s default \u2019pretty printing\u2019 mode. With this option enabled, the <small>XML<\/small> output will all be on one line (unless there are newlines in the data) \u2212 this may be easier for downstream processing.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>NoSort => 1<\/b> <i># out \u2212 seldom used<\/i> <br \/> Newer versions of XML::Simple sort elements and attributes alphabetically (*), by default. Enable this option to suppress the sorting \u2212 possibly for backwards compatibility.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">* Actually, sorting is alphabetical but \u2019key\u2019 attribute or element names (as in \u2019KeyAttr\u2019) sort first. Also, when a hash of hashes is \u2019unfolded\u2019, the elements are sorted alphabetically by the value of the key field.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>NormaliseSpace => 0 | 1 | 2<\/b> <i># in \u2212 handy<\/i> <br \/> This option controls how whitespace in text content is handled. Recognised values for the option are:<\/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>0 = (default) whitespace is passed through unaltered (except of course for the normalisation of whitespace in attribute values which is mandated by the <small>XML<\/small> recommendation)<\/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>1 = whitespace is normalised in any value used as a hash key (normalising means removing leading and trailing whitespace and collapsing sequences of whitespace characters to a single space)<\/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>2 = whitespace is normalised in all text content<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">Note: you can spell this option with a \u2019z\u2019 if that is more natural for you.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>NSExpand => 1<\/b> <i># in+out handy \u2212 <small>SAX<\/small> only<\/i> <br \/> This option controls namespace expansion \u2212 the translation of element and attribute names of the form \u2019prefix:name\u2019 to \u2019{uri}name\u2019. For example the element name \u2019xsl:template\u2019 might be expanded to: \u2019{http:\/\/www.w3.org\/1999\/XSL\/Transform}template\u2019.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">By default, &#8220;XMLin()&#8221; will return element names and attribute names exactly as they appear in the <small>XML.<\/small> Setting this option to 1 will cause all element and attribute names to be expanded to include their namespace prefix.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>Note: You must be using a <small>SAX<\/small> parser for this option to work (ie: it does not work with XML::Parser)<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This option also controls whether &#8220;XMLout()&#8221; performs the reverse translation from \u2019{uri}name\u2019 back to \u2019prefix:name\u2019. The default is no translation. If your data contains expanded names, you should set this option to 1 otherwise &#8220;XMLout&#8221; will emit <small>XML<\/small> which is not well formed.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>Note: You must have the XML::NamespaceSupport module installed if you want &#8220;XMLout()&#8221; to translate URIs back to prefixes<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>NumericEscape => 0 | 1 | 2<\/b> <i># out \u2212 handy<\/i> <br \/> Use this option to have \u2019high\u2019 (non-ASCII) characters in your Perl data structure converted to numeric entities (eg: &#8364;) in the <small>XML<\/small> output. Three levels are possible:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">0 \u2212 default: no numeric escaping ( <small>OK<\/small> if you\u2019re writing out <small>UTF8<\/small> )<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">1 \u2212 only characters above 0xFF are escaped (ie: characters in the 0x80\u2212FF range are not escaped), possibly useful with <small>ISO8859\u22121<\/small> output<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">2 \u2212 all characters above 0x7F are escaped (good for plain <small>ASCII<\/small> output)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>OutputFile => <file specifier><\/b> <i># out \u2212 handy<\/i> <br \/> The default behaviour of &#8220;XMLout()&#8221; is to return the <small>XML<\/small> as a string. If you wish to write the <small>XML<\/small> to a file, simply supply the filename using the \u2019OutputFile\u2019 option.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This option also accepts an <small>IO<\/small> handle object \u2212 especially useful in Perl 5.8.0 and later for output using an encoding other than <small>UTF\u22128,<\/small> eg:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">open my $fh, &#8216;>:encoding(iso\u22128859\u22121)&#8217;, $path or die &#8220;open($path): $!&#8221;; <br \/> XMLout($ref, OutputFile => $fh);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note, XML::Simple does not require that the object you pass in to the OutputFile option inherits from IO::Handle \u2212 it simply assumes the object supports a &#8220;print&#8221; method.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>ParserOpts => [ XML::Parser Options ]<\/b> <i># in \u2212 don\u2019t use this <br \/> Note: This option is now officially deprecated. If you find it useful, email the author with an example of what you use it for. Do not use this option to set the ProtocolEncoding, that\u2019s just plain wrong \u2212 fix the <small>XML<\/small><\/i> .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This option allows you to pass parameters to the constructor of the underlying XML::Parser object (which of course assumes you\u2019re not using <small>SAX<\/small> ).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>RootName => \u2019string\u2019<\/b> <i># out \u2212 handy<\/i> <br \/> By default, when &#8220;XMLout()&#8221; generates <small>XML,<\/small> the root element will be named \u2019opt\u2019. This option allows you to specify an alternative name.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Specifying either undef or the empty string for the RootName option will produce <small>XML<\/small> with no root elements. In most cases the resulting <small>XML<\/small> fragment will not be \u2019well formed\u2019 and therefore could not be read back in by &#8220;XMLin()&#8221;. Nevertheless, the option has been found to be useful in certain circumstances.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>SearchPath => [ list ]<\/b> <i># in \u2212 handy<\/i> <br \/> If you pass &#8220;XMLin()&#8221; a filename, but the filename include no directory component, you can use this option to specify which directories should be searched to locate the file. You might use this option to search first in the user\u2019s home directory, then in a global directory such as \/etc.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If a filename is provided to &#8220;XMLin()&#8221; but SearchPath is not defined, the file is assumed to be in the current directory.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the first parameter to &#8220;XMLin()&#8221; is undefined, the default SearchPath will contain only the directory in which the script itself is located. Otherwise the default SearchPath will be empty.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>StrictMode => 1 | 0<\/b> <i># in+out seldom used<\/i> <br \/> This option allows you to turn &#8221; <small>STRICT MODE&#8221;<\/small> on or off for a particular call, regardless of whether it was enabled at the time XML::Simple was loaded.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>SuppressEmpty => 1 | \u2019\u2019 | undef<\/b> <i># in+out \u2212 handy<\/i> <br \/> This option controls what &#8220;XMLin()&#8221; should do with empty elements (no attributes and no content). The default behaviour is to represent them as empty hashes. Setting this option to a true value (eg: 1) will cause empty elements to be skipped altogether. Setting the option to \u2019undef\u2019 or the empty string will cause empty elements to be represented as the undefined value or the empty string respectively. The latter two alternatives are a little easier to test for in your code than a hash with no keys.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The option also controls what &#8220;XMLout()&#8221; does with undefined values. Setting the option to undef causes undefined values to be output as empty elements (rather than empty attributes), it also suppresses the generation of warnings about undefined values. Setting the option to a true value (eg: 1) causes undefined values to be skipped altogether on output.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>ValueAttr => [ names ]<\/b> <i># in \u2212 handy<\/i> <br \/> Use this option to deal elements which always have a single attribute and no content. Eg:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><opt> <br \/> <colour value=\"red\" \/> <size value=\"XXL\" \/> <br \/> <\/opt><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Setting &#8220;ValueAttr => [ &#8216;value&#8217; ]&#8221; will cause the above <small>XML<\/small> to parse to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> colour => &#8216;red&#8217;, <br \/> size => &#8216;XXL&#8217; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">instead of this (the default):<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> colour => { value => &#8216;red&#8217; }, <br \/> size => { value => &#8216;XXL&#8217; } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note: This form of the ValueAttr option is not compatible with &#8220;XMLout()&#8221; \u2212 since the attribute name is discarded at parse time, the original <small>XML<\/small> cannot be reconstructed.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>ValueAttr => { element => attribute, &#8230; }<\/b> <i># in+out \u2212 handy<\/i> <br \/> This (preferred) form of the ValueAttr option requires you to specify both the element and the attribute names. This is not only safer, it also allows the original <small>XML<\/small> to be reconstructed by &#8220;XMLout()&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note: You probably don\u2019t want to use this option and the NoAttr option at the same time.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Variables => { name => value }<\/b> <i># in \u2212 handy<\/i> <br \/> This option allows variables in the <small>XML<\/small> to be expanded when the file is read. (there is no facility for putting the variable names back if you regenerate <small>XML<\/small> using &#8220;XMLout&#8221;).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A \u2019variable\u2019 is any text of the form &#8220;${name}&#8221; which occurs in an attribute value or in the text content of an element. If \u2019name\u2019 matches a key in the supplied hashref, &#8220;${name}&#8221; will be replaced with the corresponding value from the hashref. If no matching key is found, the variable will not be replaced. Names must match the regex: &#8220;[w.]+&#8221; (ie: only \u2019word\u2019 characters and dots are allowed).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>VarAttr => \u2019attr_name\u2019<\/b> <i># in \u2212 handy<\/i> <br \/> In addition to the variables defined using &#8220;Variables&#8221;, this option allows variables to be defined in the <small>XML.<\/small> A variable definition consists of an element with an attribute called \u2019attr_name\u2019 (the value of the &#8220;VarAttr&#8221; option). The value of the attribute will be used as the variable name and the text content of the element will be used as the value. A variable defined in this way will override a variable defined using the &#8220;Variables&#8221; option. For example:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">XMLin( &#8216;<opt> <br \/> <dir name=\"prefix\">\/usr\/local\/apache<\/dir> <br \/> <dir name=\"exec_prefix\">${prefix}<\/dir> <br \/> <dir name=\"bindir\">${exec_prefix}\/bin<\/dir> <br \/> <\/opt>&#8216;, <br \/> VarAttr => &#8216;name&#8217;, ContentKey => &#8216;\u2212content&#8217; <br \/> );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">produces the following data structure:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> dir => { <br \/> prefix => &#8216;\/usr\/local\/apache&#8217;, <br \/> exec_prefix => &#8216;\/usr\/local\/apache&#8217;, <br \/> bindir => &#8216;\/usr\/local\/apache\/bin&#8217;, <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>XMLDecl => 1 or XMLDecl => \u2019string\u2019<\/b> <i># out \u2212 handy<\/i> <br \/> If you want the output from &#8220;XMLout()&#8221; to start with the optional <small>XML<\/small> declaration, simply set the option to \u20191\u2019. The default <small>XML<\/small> declaration is:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><?xml version='1.0' standalone='yes'?><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you want some other string (for example to declare an encoding value), set the value of this option to the complete string you require.<\/p>\n<h2>OPTIONAL OO INTERFACE <a name=\"OPTIONAL OO INTERFACE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The procedural interface is both simple and convenient however there are a couple of reasons why you might prefer to use the object oriented ( <small>OO<\/small> ) interface:<\/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\">to define a set of default values which should be used on all subsequent calls to &#8220;XMLin()&#8221; or &#8220;XMLout()&#8221;<\/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>to override methods in <b>XML::Simple<\/b> to provide customised behaviour<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">The default values for the options described above are unlikely to suit everyone. The <small>OO<\/small> interface allows you to effectively override <b>XML::Simple<\/b>\u2019s defaults with your preferred values. It works like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">First create an XML::Simple parser object with your preferred defaults:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $xs = XML::Simple\u2212>new(ForceArray => 1, KeepRoot => 1);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">then call &#8220;XMLin()&#8221; or &#8220;XMLout()&#8221; as a method of that object:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $ref = $xs\u2212>XMLin($xml); <br \/> my $xml = $xs\u2212>XMLout($ref);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can also specify options when you make the method calls and these values will be merged with the values specified when the object was created. Values specified in a method call take precedence.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note: when called as methods, the &#8220;XMLin()&#8221; and &#8220;XMLout()&#8221; routines may be called as &#8220;xml_in()&#8221; or &#8220;xml_out()&#8221;. The method names are aliased so the only difference is the aesthetics.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Parsing Methods<\/b> <br \/> You can explicitly call one of the following methods rather than rely on the &#8220;xml_in()&#8221; method automatically determining whether the target to be parsed is a string, a file or a filehandle: <br \/> parse_string(text)<\/p>\n<p style=\"margin-left:17%;\">Works exactly like the &#8220;xml_in()&#8221; method but assumes the first argument is a string of <small>XML<\/small> (or a reference to a scalar containing a string of <small>XML<\/small> ).<\/p>\n<p style=\"margin-left:11%;\">parse_file(filename)<\/p>\n<p style=\"margin-left:17%;\">Works exactly like the &#8220;xml_in()&#8221; method but assumes the first argument is the name of a file containing <small>XML.<\/small><\/p>\n<p style=\"margin-left:11%;\">parse_fh(file_handle)<\/p>\n<p style=\"margin-left:17%;\">Works exactly like the &#8220;xml_in()&#8221; method but assumes the first argument is a filehandle which can be read to get <small>XML.<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Hook Methods<\/b> <br \/> You can make your own class which inherits from XML::Simple and overrides certain behaviours. The following methods may provide useful \u2019hooks\u2019 upon which to hang your modified behaviour. You may find other undocumented methods by examining the source, but those may be subject to change in future releases. <b><br \/> new_xml_parser()<\/b><\/p>\n<p style=\"margin-left:17%;\">This method will be called when a new XML::Parser object must be constructed (either because <small>XML::SAX<\/small> is not installed or XML::Parser is preferred).<\/p>\n<p style=\"margin-left:11%;\">handle_options(direction, name => value &#8230;)<\/p>\n<p style=\"margin-left:17%;\">This method will be called when one of the parsing methods or the &#8220;XMLout()&#8221; method is called. The initial argument will be a string (either \u2019in\u2019 or \u2019out\u2019) and the remaining arguments will be name value pairs.<\/p>\n<p style=\"margin-left:11%;\"><b>default_config_file()<\/b><\/p>\n<p style=\"margin-left:17%;\">Calculates and returns the name of the file which should be parsed if no filename is passed to &#8220;XMLin()&#8221; (default: &#8220;$0.xml&#8221;).<\/p>\n<p style=\"margin-left:11%;\">build_simple_tree(filename, string)<\/p>\n<p style=\"margin-left:17%;\">Called from &#8220;XMLin()&#8221; or any of the parsing methods. Takes either a file name as the first argument or &#8220;undef&#8221; followed by a \u2019string\u2019 as the second argument. Returns a simple tree data structure. You could override this method to apply your own transformations before the data structure is returned to the caller.<\/p>\n<p style=\"margin-left:11%;\"><b>new_hashref()<\/b><\/p>\n<p style=\"margin-left:17%;\">When the \u2019simple tree\u2019 data structure is being built, this method will be called to create any required anonymous hashrefs.<\/p>\n<p style=\"margin-left:11%;\">sorted_keys(name, hashref)<\/p>\n<p style=\"margin-left:17%;\">Called when &#8220;XMLout()&#8221; is translating a hashref to <small>XML.<\/small> This routine returns a list of hash keys in the order that the corresponding attributes\/elements should appear in the output.<\/p>\n<p style=\"margin-left:11%;\">escape_value(string)<\/p>\n<p style=\"margin-left:17%;\">Called from &#8220;XMLout()&#8221;, takes a string and returns a copy of the string with <small>XML<\/small> character escaping rules applied.<\/p>\n<p style=\"margin-left:11%;\">escape_attr(string)<\/p>\n<p style=\"margin-left:17%;\">Called from &#8220;XMLout()&#8221;, to handle attribute values. By default, just calls &#8220;escape_value()&#8221;, but you can override this method if you want attributes escaped differently than text content.<\/p>\n<p style=\"margin-left:11%;\">numeric_escape(string)<\/p>\n<p style=\"margin-left:17%;\">Called from &#8220;escape_value()&#8221;, to handle non-ASCII characters (depending on the value of the NumericEscape option).<\/p>\n<p style=\"margin-left:11%;\">copy_hash(hashref, extra_key => value, &#8230;)<\/p>\n<p style=\"margin-left:17%;\">Called from &#8220;XMLout()&#8221;, when \u2019unfolding\u2019 a hash of hashes into an array of hashes. You might wish to override this method if you\u2019re using tied hashes and don\u2019t want them to get untied.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Cache Methods<\/b> <br \/> XML::Simple implements three caching schemes (\u2019storable\u2019, \u2019memshare\u2019 and \u2019memcopy\u2019). You can implement a custom caching scheme by implementing two methods \u2212 one for reading from the cache and one for writing to it.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For example, you might implement a new \u2019dbm\u2019 scheme that stores cached data structures using the <small>MLDBM<\/small> module. First, you would add a &#8220;cache_read_dbm()&#8221; method which accepted a filename for use as a lookup key and returned a data structure on success, or undef on failure. Then, you would implement a &#8220;cache_read_dbm()&#8221; method which accepted a data structure and a filename.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You would use this caching scheme by specifying the option:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Cache => [ &#8216;dbm&#8217; ]<\/p>\n<h2>STRICT MODE <a name=\"STRICT MODE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">If you import the <b>XML::Simple<\/b> routines like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use XML::Simple qw(:strict);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">the following common mistakes will be detected and treated as fatal errors<\/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>Failing to explicitly set the &#8220;KeyAttr&#8221; option \u2212 if you can\u2019t be bothered reading about this option, turn it off with: KeyAttr => [ ]<\/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>Failing to explicitly set the &#8220;ForceArray&#8221; option \u2212 if you can\u2019t be bothered reading about this option, set it to the safest mode with: ForceArray => 1<\/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>Setting ForceArray to an array, but failing to list all the elements from the KeyAttr 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>Data error \u2212 KeyAttr is set to say { part => \u2019partnum\u2019 } but the <small>XML<\/small> contains one or more <part> elements without a \u2019partnum\u2019 attribute (or nested element). Note: if strict mode is not set but &#8220;use warnings;&#8221; is in force, this condition triggers a warning.<\/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>Data error \u2212 as above, but non-unique values are present in the key attribute (eg: more than one <part> element with the same partnum). This will also trigger a warning if strict mode is not enabled.<\/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>Data error \u2212 as above, but value of key attribute (eg: partnum) is not a scalar string (due to nested elements etc). This will also trigger a warning if strict mode is not enabled.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<h2>SAX SUPPORT <a name=\"SAX SUPPORT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">From version 1.08_01, <b>XML::Simple<\/b> includes support for <small>SAX<\/small> (the Simple <small>API<\/small> for <small>XML<\/small> ) \u2212 specifically <small>SAX2.<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In a typical <small>SAX<\/small> application, an <small>XML<\/small> parser (or <small>SAX<\/small> \u2019driver\u2019) module generates <small>SAX<\/small> events (start of element, character data, end of element, etc) as it parses an <small>XML<\/small> document and a \u2019handler\u2019 module processes the events to extract the required data. This simple model allows for some interesting and powerful possibilities:<\/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\">Applications written to the <small>SAX API<\/small> can extract data from huge <small>XML<\/small> documents without the memory overheads of a <small>DOM<\/small> or tree <small>API.<\/small><\/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>The <small>SAX API<\/small> allows for plug and play interchange of parser modules without having to change your code to fit a new module\u2019s <small>API.<\/small> A number of <small>SAX<\/small> parsers are available with capabilities ranging from extreme portability to blazing performance.<\/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>A <small>SAX<\/small> \u2019filter\u2019 module can implement both a handler interface for receiving data and a generator interface for passing modified data on to a downstream handler. Filters can be chained together in \u2019pipelines\u2019.<\/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>One filter module might split a data stream to direct data to two or more downstream handlers.<\/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>Generating <small>SAX<\/small> events is not the exclusive preserve of <small>XML<\/small> parsing modules. For example, a module might extract data from a relational database using <small>DBI<\/small> and pass it on to a <small>SAX<\/small> pipeline for filtering and formatting.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>XML::Simple<\/b> can operate at either end of a <small>SAX<\/small> pipeline. For example, you can take a data structure in the form of a hashref and pass it into a <small>SAX<\/small> pipeline using the \u2019Handler\u2019 option on &#8220;XMLout()&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use XML::Simple; <br \/> use Some::SAX::Filter; <br \/> use XML::SAX::Writer; <br \/> my $ref = { <br \/> &#8230;. # your data here <br \/> }; <br \/> my $writer = XML::SAX::Writer\u2212>new(); <br \/> my $filter = Some::SAX::Filter\u2212>new(Handler => $writer); <br \/> my $simple = XML::Simple\u2212>new(Handler => $filter); <br \/> $simple\u2212>XMLout($ref);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can also put <b>XML::Simple<\/b> at the opposite end of the pipeline to take advantage of the simple \u2019tree\u2019 data structure once the relevant data has been isolated through filtering:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use XML::SAX; <br \/> use Some::SAX::Filter; <br \/> use XML::Simple; <br \/> my $simple = XML::Simple\u2212>new(ForceArray => 1, KeyAttr => [&#8216;partnum&#8217;]); <br \/> my $filter = Some::SAX::Filter\u2212>new(Handler => $simple); <br \/> my $parser = XML::SAX::ParserFactory\u2212>parser(Handler => $filter); <br \/> my $ref = $parser\u2212>parse_uri(&#8216;some_huge_file.xml&#8217;); <br \/> print $ref\u2212>{part}\u2212>{&#8216;555\u22121234&#8217;};<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can build a filter by using an XML::Simple object as a handler and setting its DataHandler option to point to a routine which takes the resulting tree, modifies it and sends it off as <small>SAX<\/small> events to a downstream handler:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $writer = XML::SAX::Writer\u2212>new(); <br \/> my $filter = XML::Simple\u2212>new( <br \/> DataHandler => sub { <br \/> my $simple = shift; <br \/> my $data = shift; <br \/> # Modify $data here <br \/> $simple\u2212>XMLout($data, Handler => $writer); <br \/> } <br \/> ); <br \/> my $parser = XML::SAX::ParserFactory\u2212>parser(Handler => $filter); <br \/> $parser\u2212>parse_uri($filename);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>Note: In this last example, the \u2019Handler\u2019 option was specified in the call to &#8220;XMLout()&#8221; but it could also have been specified in the constructor<\/i>.<\/p>\n<h2>ENVIRONMENT <a name=\"ENVIRONMENT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">If you don\u2019t care which parser module <b>XML::Simple<\/b> uses then skip this section entirely (it looks more complicated than it really is).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>XML::Simple<\/b> will default to using a <b><small>SAX<\/small><\/b> parser if one is available or <b>XML::Parser<\/b> if <small>SAX<\/small> is not available.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can dictate which parser module is used by setting either the environment variable \u2019 <small>XML_SIMPLE_PREFERRED_PARSER<\/small> \u2019 or the package variable $XML::Simple::PREFERRED_PARSER to contain the module name. The following rules are used:<\/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\">The package variable takes precedence over the environment variable if both are defined. To force <b>XML::Simple<\/b> to ignore the environment settings and use its default rules, you can set the package variable to an empty string.<\/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 the \u2019preferred parser\u2019 is set to the string \u2019XML::Parser\u2019, then XML::Parser will be used (or &#8220;XMLin()&#8221; will die if XML::Parser is not installed).<\/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 the \u2019preferred parser\u2019 is set to some other value, then it is assumed to be the name of a <small>SAX<\/small> parser module and is passed to XML::SAX::ParserFactory. If <small>XML::SAX<\/small> is not installed, or the requested parser module is not installed, then &#8220;XMLin()&#8221; will die.<\/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 the \u2019preferred parser\u2019 is not defined at all (the normal default state), an attempt will be made to load <small>XML::SAX<\/small> . If <small>XML::SAX<\/small> is installed, then a parser module will be selected according to XML::SAX::ParserFactory\u2019s normal rules (which typically means the last <small>SAX<\/small> parser installed).<\/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 the \u2019preferred parser\u2019 is not defined and <b><small>XML::SAX<\/small><\/b> is not installed, then <b>XML::Parser<\/b> will be used. &#8220;XMLin()&#8221; will die if XML::Parser is not installed.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">Note: The <b><small>XML::SAX<\/small><\/b> distribution includes an <small>XML<\/small> parser written entirely in Perl. It is very portable but it is not very fast. You should consider installing XML::LibXML or XML::SAX::Expat if they are available for your platform.<\/p>\n<h2>ERROR HANDLING <a name=\"ERROR HANDLING\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <small>XML<\/small> standard is very clear on the issue of non-compliant documents. An error in parsing any single element (for example a missing end tag) must cause the whole document to be rejected. <b>XML::Simple<\/b> will die with an appropriate message if it encounters a parsing error.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If dying is not appropriate for your application, you should arrange to call &#8220;XMLin()&#8221; in an eval block and look for errors in $@. eg:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $config = eval { XMLin() }; <br \/> PopUpMessage($@) if($@);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note, there is a common misconception that use of <b>eval<\/b> will significantly slow down a script. While that may be true when the code being eval\u2019d is in a string, it is not true of code like the sample above.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">When &#8220;XMLin()&#8221; reads the following very simple piece of <small>XML:<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><opt username=\"testuser\" password=\"frodo\"><\/opt><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">it returns the following data structure:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> &#8216;username&#8217; => &#8216;testuser&#8217;, <br \/> &#8216;password&#8217; => &#8216;frodo&#8217; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The identical result could have been produced with this alternative <small>XML:<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><opt username=\"testuser\" password=\"frodo\" \/><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Or this (although see \u2019ForceArray\u2019 option for variations):<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><opt> <br \/> <username>testuser<\/username>  <password>frodo<\/password> <\/opt><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Repeated nested elements are represented as anonymous arrays:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><opt>  <person firstname=\"Joe\" lastname=\"Smith\"> <email>joe@smith.com<\/email> <br \/> <email>jsmith@yahoo.com<\/email>  <\/person> <person firstname=\"Bob\" lastname=\"Smith\"> <email>bob@smith.com<\/email>  <\/person> <\/opt> <br \/> { <br \/> &#8216;person&#8217; => [ <br \/> { <br \/> &#8216;email&#8217; => [ <br \/> &#8216;joe@smith.com&#8217;, <br \/> &#8216;jsmith@yahoo.com&#8217; <br \/> ], <br \/> &#8216;firstname&#8217; => &#8216;Joe&#8217;, <br \/> &#8216;lastname&#8217; => &#8216;Smith&#8217; <br \/> }, <br \/> { <br \/> &#8216;email&#8217; => &#8216;bob@smith.com&#8217;, <br \/> &#8216;firstname&#8217; => &#8216;Bob&#8217;, <br \/> &#8216;lastname&#8217; => &#8216;Smith&#8217; <br \/> } <br \/> ] <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Nested elements with a recognised key attribute are transformed (folded) from an array into a hash keyed on the value of that attribute (see the &#8220;KeyAttr&#8221; option):<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><opt>  <person key=\"jsmith\" firstname=\"Joe\" lastname=\"Smith\" \/> <person key=\"tsmith\" firstname=\"Tom\" lastname=\"Smith\" \/> <person key=\"jbloggs\" firstname=\"Joe\" lastname=\"Bloggs\" \/> <\/opt> <br \/> { <br \/> &#8216;person&#8217; => { <br \/> &#8216;jbloggs&#8217; => { <br \/> &#8216;firstname&#8217; => &#8216;Joe&#8217;, <br \/> &#8216;lastname&#8217; => &#8216;Bloggs&#8217; <br \/> }, <br \/> &#8216;tsmith&#8217; => { <br \/> &#8216;firstname&#8217; => &#8216;Tom&#8217;, <br \/> &#8216;lastname&#8217; => &#8216;Smith&#8217; <br \/> }, <br \/> &#8216;jsmith&#8217; => { <br \/> &#8216;firstname&#8217; => &#8216;Joe&#8217;, <br \/> &#8216;lastname&#8217; => &#8216;Smith&#8217; <br \/> } <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <anon> tag can be used to form anonymous arrays:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><opt> <br \/> <head><anon>Col 1<\/anon><anon>Col 2<\/anon><anon>Col 3<\/anon><\/head> <br \/> <data><anon>R1C1<\/anon><anon>R1C2<\/anon><anon>R1C3<\/anon><\/data> <br \/> <data><anon>R2C1<\/anon><anon>R2C2<\/anon><anon>R2C3<\/anon><\/data> <br \/> <data><anon>R3C1<\/anon><anon>R3C2<\/anon><anon>R3C3<\/anon><\/data> <br \/> <\/opt> <br \/> { <br \/> &#8216;head&#8217; => [ <br \/> [ &#8216;Col 1&#8217;, &#8216;Col 2&#8217;, &#8216;Col 3&#8217; ] <br \/> ], <br \/> &#8216;data&#8217; => [ <br \/> [ &#8216;R1C1&#8217;, &#8216;R1C2&#8217;, &#8216;R1C3&#8217; ], <br \/> [ &#8216;R2C1&#8217;, &#8216;R2C2&#8217;, &#8216;R2C3&#8217; ], <br \/> [ &#8216;R3C1&#8217;, &#8216;R3C2&#8217;, &#8216;R3C3&#8217; ] <br \/> ] <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Anonymous arrays can be nested to arbitrary levels and as a special case, if the surrounding tags for an <small>XML<\/small> document contain only an anonymous array the arrayref will be returned directly rather than the usual hashref:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><opt> <br \/> <anon><anon>Col 1<\/anon><anon>Col 2<\/anon><\/anon> <br \/> <anon><anon>R1C1<\/anon><anon>R1C2<\/anon><\/anon> <br \/> <anon><anon>R2C1<\/anon><anon>R2C2<\/anon><\/anon> <br \/> <\/opt> <br \/> [ <br \/> [ &#8216;Col 1&#8217;, &#8216;Col 2&#8217; ], <br \/> [ &#8216;R1C1&#8217;, &#8216;R1C2&#8217; ], <br \/> [ &#8216;R2C1&#8217;, &#8216;R2C2&#8217; ] <br \/> ]<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Elements which only contain text content will simply be represented as a scalar. Where an element has both attributes and text content, the element will be represented as a hashref with the text content in the \u2019content\u2019 key (see the &#8220;ContentKey&#8221; option):<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><opt> <br \/> <one>first<\/one> <br \/> <two attr=\"value\">second<\/two> <br \/> <\/opt> <br \/> { <br \/> &#8216;one&#8217; => &#8216;first&#8217;, <br \/> &#8216;two&#8217; => { &#8216;attr&#8217; => &#8216;value&#8217;, &#8216;content&#8217; => &#8216;second&#8217; } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Mixed content (elements which contain both text content and nested elements) will be not be represented in a useful way \u2212 element order and significant whitespace will be lost. If you need to work with mixed content, then XML::Simple is not the right tool for your job \u2212 check out the next section.<\/p>\n<h2>WHERE TO FROM HERE? <a name=\"WHERE TO FROM HERE?\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>XML::Simple<\/b> is able to present a simple <small>API<\/small> because it makes some assumptions on your behalf. These 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 style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">You\u2019re not interested in text content consisting only of whitespace<\/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>You don\u2019t mind that when things get slurped into a hash the order is lost<\/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>You don\u2019t want fine-grained control of the formatting of generated <small>XML<\/small><\/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>You would never use a hash key that was not a legal <small>XML<\/small> element name<\/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>You don\u2019t need help converting between different encodings<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">In a serious <small>XML<\/small> project, you\u2019ll probably outgrow these assumptions fairly quickly. This section of the document used to offer some advice on choosing a more powerful option. That advice has now grown into the \u2019Perl\u2212XML <small>FAQ<\/small> \u2019 document which you can find at: <http:\/\/perl\u2212xml.sourceforge.net\/faq\/><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The advice in the <small>FAQ<\/small> boils down to a quick explanation of tree versus event based parsers and then recommends:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For event based parsing, use <small>SAX<\/small> (do not set out to write any new code for XML::Parser\u2019s handler <small>API<\/small> \u2212 it is obsolete).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For tree-based parsing, you could choose between the \u2019Perlish\u2019 approach of XML::Twig and more standards based <small>DOM<\/small> implementations \u2212 preferably one with XPath support such as XML::LibXML.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>XML::Simple<\/b> requires either XML::Parser or <small>XML::SAX<\/small> .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">To generate documents with namespaces, XML::NamespaceSupport is required.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The optional caching functions require Storable.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Answers to Frequently Asked Questions about XML::Simple are bundled with this distribution as: XML::Simple::FAQ<\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Copyright 1999\u22122004 Grant McLean <grantm@cpan.org><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This library is free software; you can redistribute it and\/or modify it under the same terms as Perl itself.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  XML::Simple \u2212 An API for simple XML files <\/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,3331],"class_list":["post-6981","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-man3","tag-xmlsimple"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6981","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=6981"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6981\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}