Server IP : 103.119.228.120 / Your IP : 3.145.8.139 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/ssl/local/ssl/local/ssl/local/ssl/local/ssl/local/share/man/man3/ |
Upload File : |
.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "XML::Simple 3" .TH XML::Simple 3 "2015-12-05" "perl v5.16.3" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" XML::Simple \- An API for simple XML files .SH "SYNOPSIS" .IX Header "SYNOPSIS" You really don't want to use this module in new code. If you ignore this warning and use it anyway, the \f(CW\*(C`qw(:strict)\*(C'\fR mode will save you a little pain. .PP .Vb 1 \& use XML::Simple qw(:strict); \& \& my $ref = XMLin([<xml file or string>] [, <options>]); \& \& my $xml = XMLout($hashref [, <options>]); .Ve .PP Or the object oriented way: .PP .Vb 1 \& require XML::Simple qw(:strict); \& \& my $xs = XML::Simple\->new([<options>]); \& \& my $ref = $xs\->XMLin([<xml file or string>] [, <options>]); \& \& my $xml = $xs\->XMLout($hashref [, <options>]); .Ve .PP (or see \*(L"\s-1SAX SUPPORT\*(R"\s0 for 'the \s-1SAX\s0 way'). .PP Note, in these examples, the square brackets are used to denote optional items not to imply items should be supplied in arrayrefs. .SH "STATUS OF THIS MODULE" .IX Header "STATUS OF THIS MODULE" The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended and XML::Twig is an excellent alternative. .PP 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 \- often producing unexpected results. .PP Patches with bug fixes and documentation fixes are welcome, but new features are unlikely to be added. .SH "QUICK START" .IX Header "QUICK START" Say you have a script called \fBfoo\fR and a file of configuration options called \fBfoo.xml\fR containing the following: .PP .Vb 10 \& <config logdir="/var/log/foo/" debugfile="/tmp/foo.debug"> \& <server name="sahara" osname="solaris" osversion="2.6"> \& <address>10.0.0.101</address> \& <address>10.0.1.101</address> \& </server> \& <server name="gobi" osname="irix" osversion="6.5"> \& <address>10.0.0.102</address> \& </server> \& <server name="kalahari" osname="linux" osversion="2.0.34"> \& <address>10.0.0.103</address> \& <address>10.0.1.103</address> \& </server> \& </config> .Ve .PP The following lines of code in \fBfoo\fR: .PP .Vb 1 \& use XML::Simple qw(:strict); \& \& my $config = XMLin(undef, KeyAttr => { server => \*(Aqname\*(Aq }, ForceArray => [ \*(Aqserver\*(Aq, \*(Aqaddress\*(Aq ]); .Ve .PP will 'slurp' the configuration options into the hashref \f(CW$config\fR (because no filename or \s-1XML\s0 string was passed as the first argument to \f(CW\*(C`XMLin()\*(C'\fR the name and location of the \s-1XML\s0 file will be inferred from name and location of the script). You can dump out the contents of the hashref using Data::Dumper: .PP .Vb 1 \& use Data::Dumper; \& \& print Dumper($config); .Ve .PP which will produce something like this (formatting has been adjusted for brevity): .PP .Vb 10 \& { \& \*(Aqlogdir\*(Aq => \*(Aq/var/log/foo/\*(Aq, \& \*(Aqdebugfile\*(Aq => \*(Aq/tmp/foo.debug\*(Aq, \& \*(Aqserver\*(Aq => { \& \*(Aqsahara\*(Aq => { \& \*(Aqosversion\*(Aq => \*(Aq2.6\*(Aq, \& \*(Aqosname\*(Aq => \*(Aqsolaris\*(Aq, \& \*(Aqaddress\*(Aq => [ \*(Aq10.0.0.101\*(Aq, \*(Aq10.0.1.101\*(Aq ] \& }, \& \*(Aqgobi\*(Aq => { \& \*(Aqosversion\*(Aq => \*(Aq6.5\*(Aq, \& \*(Aqosname\*(Aq => \*(Aqirix\*(Aq, \& \*(Aqaddress\*(Aq => [ \*(Aq10.0.0.102\*(Aq ] \& }, \& \*(Aqkalahari\*(Aq => { \& \*(Aqosversion\*(Aq => \*(Aq2.0.34\*(Aq, \& \*(Aqosname\*(Aq => \*(Aqlinux\*(Aq, \& \*(Aqaddress\*(Aq => [ \*(Aq10.0.0.103\*(Aq, \*(Aq10.0.1.103\*(Aq ] \& } \& } \& } .Ve .PP Your script could then access the name of the log directory like this: .PP .Vb 1 \& print $config\->{logdir}; .Ve .PP similarly, the second address on the server 'kalahari' could be referenced as: .PP .Vb 1 \& print $config\->{server}\->{kalahari}\->{address}\->[1]; .Ve .PP 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 'references' tutorial (\s-1AKA: \s0\*(L"Mark's very short tutorial about references\*(R") at perlreftut. .PP In this example, the \f(CW\*(C`ForceArray\*(C'\fR 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). .PP The \f(CW\*(C`KeyAttr\*(C'\fR option was used to indicate that each \f(CW\*(C`<server>\*(C'\fR element has a unique identifier in the \f(CW\*(C`name\*(C'\fR attribute. This allows you to index directly to a particular server record using the name as a hash key (as shown above). .PP For simple requirements, that's really all there is to it. If you want to store your \s-1XML\s0 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'll need to check out \&\*(L"\s-1OPTIONS\*(R"\s0. If you want to turn off or tweak the array folding feature (that neat little transformation that produced \f(CW$config\fR\->{server}) you'll find options for that as well. .PP If you want to generate \s-1XML \s0(for example to write a modified version of \&\f(CW$config\fR back out as \s-1XML\s0), check out \f(CW\*(C`XMLout()\*(C'\fR. .PP If your needs are not so simple, this may not be the module for you. In that case, you might want to read \*(L"\s-1WHERE TO FROM HERE\s0?\*(R". .SH "DESCRIPTION" .IX Header "DESCRIPTION" The XML::Simple module provides a simple \s-1API\s0 layer on top of an underlying \s-1XML\s0 parsing module (either XML::Parser or one of the \s-1SAX2\s0 parser modules). Two functions are exported: \f(CW\*(C`XMLin()\*(C'\fR and \f(CW\*(C`XMLout()\*(C'\fR. Note: you can explicitly request the lower case versions of the function names: \f(CW\*(C`xml_in()\*(C'\fR and \&\f(CW\*(C`xml_out()\*(C'\fR. .PP The simplest approach is to call these two functions directly, but an optional object oriented interface (see \*(L"\s-1OPTIONAL OO INTERFACE\*(R"\s0 below) allows them to be called as methods of an \fBXML::Simple\fR object. The object interface can also be used at either end of a \s-1SAX\s0 pipeline. .SS "\fIXMLin()\fP" .IX Subsection "XMLin()" Parses \s-1XML\s0 formatted data and returns a reference to a data structure which contains the same information in a more readily accessible form. (Skip down to \*(L"\s-1EXAMPLES\*(R"\s0 below, for more sample code). .PP \&\f(CW\*(C`XMLin()\*(C'\fR accepts an optional \s-1XML\s0 specifier followed by zero or more 'name => value' option pairs. The \s-1XML\s0 specifier can be one of the following: .IP "A filename" 4 .IX Item "A filename" If the filename contains no directory components \f(CW\*(C`XMLin()\*(C'\fR will look for the file in each directory in the SearchPath (see \*(L"\s-1OPTIONS\*(R"\s0 below) or in the current directory if the SearchPath option is not defined. eg: .Sp .Vb 1 \& $ref = XMLin(\*(Aq/etc/params.xml\*(Aq); .Ve .Sp Note, the filename '\-' can be used to parse from \s-1STDIN.\s0 .IP "undef" 4 .IX Item "undef" If there is no \s-1XML\s0 specifier, \f(CW\*(C`XMLin()\*(C'\fR 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 '.xml'. Note: if you wish to specify options, you must specify the value 'undef'. eg: .Sp .Vb 1 \& $ref = XMLin(undef, ForceArray => 1); .Ve .IP "A string of \s-1XML\s0" 4 .IX Item "A string of XML" A string containing \s-1XML \s0(recognised by the presence of '<' and '>' characters) will be parsed directly. eg: .Sp .Vb 1 \& $ref = XMLin(\*(Aq<opt username="bob" password="flurp" />\*(Aq); .Ve .IP "An IO::Handle object" 4 .IX Item "An IO::Handle object" An IO::Handle object will be read to \s-1EOF\s0 and its contents parsed. eg: .Sp .Vb 2 \& $fh = IO::File\->new(\*(Aq/etc/params.xml\*(Aq); \& $ref = XMLin($fh); .Ve .SS "\fIXMLout()\fP" .IX Subsection "XMLout()" Takes a data structure (generally a hashref) and returns an \s-1XML\s0 encoding of that structure. If the resulting \s-1XML\s0 is parsed using \f(CW\*(C`XMLin()\*(C'\fR, it should return a data structure equivalent to the original (see caveats below). .PP The \f(CW\*(C`XMLout()\*(C'\fR function can also be used to output the \s-1XML\s0 as \s-1SAX\s0 events see the \f(CW\*(C`Handler\*(C'\fR option and \*(L"\s-1SAX SUPPORT\*(R"\s0 for more details). .PP When translating hashes to \s-1XML,\s0 hash keys which have a leading '\-' will be silently skipped. This is the approved method for marking elements of a data structure which should be ignored by \f(CW\*(C`XMLout\*(C'\fR. (Note: If these items were not skipped the key names would be emitted as element or attribute names with a leading '\-' which would not be valid \s-1XML\s0). .SS "Caveats" .IX Subsection "Caveats" Some care is required in creating data structures which will be passed to \&\f(CW\*(C`XMLout()\*(C'\fR. Hash keys from the data structure will be encoded as either \s-1XML\s0 element names or attribute names. Therefore, you should use hash key names which conform to the relatively strict \s-1XML\s0 naming rules: .PP Names in \s-1XML\s0 must begin with a letter. The remaining characters may be letters, digits, hyphens (\-), 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 (\fBXML::Simple\fR can only usefully work with namespaces when teamed with a \s-1SAX\s0 Parser). .PP You can use other punctuation characters in hash values (just not in hash keys) however \fBXML::Simple\fR does not support dumping binary data. .PP If you break these rules, the current implementation of \f(CW\*(C`XMLout()\*(C'\fR will simply emit non-compliant \s-1XML\s0 which will be rejected if you try to read it back in. (A later version of \fBXML::Simple\fR might take a more proactive approach). .PP Note also that although you can nest hashes and arrays to arbitrary levels, circular data structures are not supported and will cause \f(CW\*(C`XMLout()\*(C'\fR to die. .PP If you wish to 'round\-trip' arbitrary data structures from Perl to \s-1XML\s0 and back to Perl, then you should probably disable array folding (using the KeyAttr option) both with \f(CW\*(C`XMLout()\*(C'\fR and with \f(CW\*(C`XMLin()\*(C'\fR. If you still don't get the expected results, you may prefer to use XML::Dumper which is designed for exactly that purpose. .PP Refer to \*(L"\s-1WHERE TO FROM HERE\s0?\*(R" if \f(CW\*(C`XMLout()\*(C'\fR is too simple for your needs. .SH "OPTIONS" .IX Header "OPTIONS" \&\fBXML::Simple\fR supports a number of options (in fact as each release of \&\fBXML::Simple\fR adds more options, the module's claim to the name 'Simple' becomes increasingly tenuous). If you find yourself repeatedly having to specify the same options, you might like to investigate \*(L"\s-1OPTIONAL OO INTERFACE\*(R"\s0 below. .PP If you can't be bothered reading the documentation, refer to \&\*(L"\s-1STRICT MODE\*(R"\s0 to automatically catch common mistakes. .PP Because there are so many options, it's hard for new users to know which ones are important, so here are the two you really need to know about: .IP "\(bu" 4 check out \f(CW\*(C`ForceArray\*(C'\fR because you'll almost certainly want to turn it on .IP "\(bu" 4 make sure you know what the \f(CW\*(C`KeyAttr\*(C'\fR option does and what its default value is because it may surprise you otherwise (note in particular that 'KeyAttr' affects both \f(CW\*(C`XMLin\*(C'\fR and \f(CW\*(C`XMLout\*(C'\fR) .PP The option name headings below have a trailing 'comment' \- a hash followed by two pieces of metadata: .IP "\(bu" 4 Options are marked with '\fIin\fR' if they are recognised by \f(CW\*(C`XMLin()\*(C'\fR and \&'\fIout\fR' if they are recognised by \f(CW\*(C`XMLout()\*(C'\fR. .IP "\(bu" 4 Each option is also flagged to indicate whether it is: .Sp .Vb 7 \& \*(Aqimportant\*(Aq \- don\*(Aqt use the module until you understand this one \& \*(Aqhandy\*(Aq \- you can skip this on the first time through \& \*(Aqadvanced\*(Aq \- you can skip this on the second time through \& \*(AqSAX only\*(Aq \- don\*(Aqt worry about this unless you\*(Aqre using SAX (or \& alternatively if you need this, you also need SAX) \& \*(Aqseldom used\*(Aq \- you\*(Aqll probably never use this unless you were the \& person that requested the feature .Ve .PP The options are listed alphabetically: .PP 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). .SS "AttrIndent => 1 \fI# out \- handy\fP" .IX Subsection "AttrIndent => 1 # out - handy" When you are using \f(CW\*(C`XMLout()\*(C'\fR, enable this option to have attributes printed one-per-line with sensible indentation rather than all on one line. .SS "Cache => [ cache schemes ] \fI# in \- advanced\fP" .IX Subsection "Cache => [ cache schemes ] # in - advanced" Because loading the \fBXML::Parser\fR module and parsing an \s-1XML\s0 file can consume a significant number of \s-1CPU\s0 cycles, it is often desirable to cache the output of \&\f(CW\*(C`XMLin()\*(C'\fR for later reuse. .PP When parsing from a named file, \fBXML::Simple\fR supports a number of caching schemes. The 'Cache' 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 \s-1XML\s0 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: .IP "storable" 4 .IX Item "storable" Utilises \fBStorable.pm\fR to read/write a cache file with the same name as the \&\s-1XML\s0 file but with the extension .stor .IP "memshare" 4 .IX Item "memshare" When a file is first parsed, a copy of the resulting data structure is retained in memory in the \fBXML::Simple\fR module's 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). .Sp 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. .IP "memcopy" 4 .IX Item "memcopy" This scheme works identically to 'memshare' (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 \fBStorable.pm\fR to perform the copy. .PP 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 \s-1NFS\s0 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 \s-1XML\s0 file may appear to be ignored. .SS "ContentKey => 'keyname' \fI# in+out \- seldom used\fP" .IX Subsection "ContentKey => 'keyname' # in+out - seldom used" When text content is parsed to a hash value, this option lets you specify a name for the hash key to override the default 'content'. So for example: .PP .Vb 1 \& XMLin(\*(Aq<opt one="1">Text</opt>\*(Aq, ContentKey => \*(Aqtext\*(Aq) .Ve .PP will parse to: .PP .Vb 1 \& { \*(Aqone\*(Aq => 1, \*(Aqtext\*(Aq => \*(AqText\*(Aq } .Ve .PP instead of: .PP .Vb 1 \& { \*(Aqone\*(Aq => 1, \*(Aqcontent\*(Aq => \*(AqText\*(Aq } .Ve .PP \&\f(CW\*(C`XMLout()\*(C'\fR will also honour the value of this option when converting a hashref to \s-1XML.\s0 .PP You can also prefix your selected key name with a '\-' character to have \&\f(CW\*(C`XMLin()\*(C'\fR try a little harder to eliminate unnecessary 'content' keys after array folding. For example: .PP .Vb 6 \& XMLin( \& \*(Aq<opt><item name="one">First</item><item name="two">Second</item></opt>\*(Aq, \& KeyAttr => {item => \*(Aqname\*(Aq}, \& ForceArray => [ \*(Aqitem\*(Aq ], \& ContentKey => \*(Aq\-content\*(Aq \& ) .Ve .PP will parse to: .PP .Vb 6 \& { \& \*(Aqitem\*(Aq => { \& \*(Aqone\*(Aq => \*(AqFirst\*(Aq \& \*(Aqtwo\*(Aq => \*(AqSecond\*(Aq \& } \& } .Ve .PP rather than this (without the '\-'): .PP .Vb 6 \& { \& \*(Aqitem\*(Aq => { \& \*(Aqone\*(Aq => { \*(Aqcontent\*(Aq => \*(AqFirst\*(Aq } \& \*(Aqtwo\*(Aq => { \*(Aqcontent\*(Aq => \*(AqSecond\*(Aq } \& } \& } .Ve .SS "DataHandler => code_ref \fI# in \- \s-1SAX\s0 only\fP" .IX Subsection "DataHandler => code_ref # in - SAX only" When you use an \fBXML::Simple\fR object as a \s-1SAX\s0 handler, it will return a \&'simple tree' data structure in the same format as \f(CW\*(C`XMLin()\*(C'\fR 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 \&\fBXML::Simple\fR object and a reference to the data tree. The return value from the subroutine will be returned to the \s-1SAX\s0 driver. (See \*(L"\s-1SAX SUPPORT\*(R"\s0 for more details). .SS "ForceArray => 1 \fI# in \- important\fP" .IX Subsection "ForceArray => 1 # in - important" This option should be set to '1' to force nested elements to be represented as arrays even when there is only one. Eg, with ForceArray enabled, this \&\s-1XML:\s0 .PP .Vb 3 \& <opt> \& <name>value</name> \& </opt> .Ve .PP would parse to this: .PP .Vb 5 \& { \& \*(Aqname\*(Aq => [ \& \*(Aqvalue\*(Aq \& ] \& } .Ve .PP instead of this (the default): .PP .Vb 3 \& { \& \*(Aqname\*(Aq => \*(Aqvalue\*(Aq \& } .Ve .PP This option is especially useful if the data structure is likely to be written back out as \s-1XML\s0 and the default behaviour of rolling single nested elements up into attributes is not desirable. .PP 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 'KeyAttr' enables array folding, the default value of this option should probably also have been enabled too \- sorry). .SS "ForceArray => [ names ] \fI# in \- important\fP" .IX Subsection "ForceArray => [ names ] # in - important" This alternative (and preferred) form of the 'ForceArray' option allows you to specify a list of element names which should always be forced into an array representation, rather than the 'all or nothing' approach above. .PP It is also possible (since version 2.05) to include compiled regular expressions in the list \- 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: .PP .Vb 1 \& ForceArray => qr/_list$/ .Ve .SS "ForceContent => 1 \fI# in \- seldom used\fP" .IX Subsection "ForceContent => 1 # in - seldom used" When \f(CW\*(C`XMLin()\*(C'\fR 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: .PP .Vb 1 \& XMLin(\*(Aq<opt><x>text1</x><y a="2">text2</y></opt>\*(Aq, ForceContent => 1) .Ve .PP will parse to: .PP .Vb 4 \& { \& \*(Aqx\*(Aq => { \*(Aqcontent\*(Aq => \*(Aqtext1\*(Aq }, \& \*(Aqy\*(Aq => { \*(Aqa\*(Aq => 2, \*(Aqcontent\*(Aq => \*(Aqtext2\*(Aq } \& } .Ve .PP instead of: .PP .Vb 4 \& { \& \*(Aqx\*(Aq => \*(Aqtext1\*(Aq, \& \*(Aqy\*(Aq => { \*(Aqa\*(Aq => 2, \*(Aqcontent\*(Aq => \*(Aqtext2\*(Aq } \& } .Ve .SS "GroupTags => { grouping tag => grouped tag } \fI# in+out \- handy\fP" .IX Subsection "GroupTags => { grouping tag => grouped tag } # in+out - handy" You can use this option to eliminate extra levels of indirection in your Perl data structure. For example this \s-1XML:\s0 .PP .Vb 7 \& <opt> \& <searchpath> \& <dir>/usr/bin</dir> \& <dir>/usr/local/bin</dir> \& <dir>/usr/X11/bin</dir> \& </searchpath> \& </opt> .Ve .PP Would normally be read into a structure like this: .PP .Vb 5 \& { \& searchpath => { \& dir => [ \*(Aq/usr/bin\*(Aq, \*(Aq/usr/local/bin\*(Aq, \*(Aq/usr/X11/bin\*(Aq ] \& } \& } .Ve .PP But when read in with the appropriate value for 'GroupTags': .PP .Vb 1 \& my $opt = XMLin($xml, GroupTags => { searchpath => \*(Aqdir\*(Aq }); .Ve .PP It will return this simpler structure: .PP .Vb 3 \& { \& searchpath => [ \*(Aq/usr/bin\*(Aq, \*(Aq/usr/local/bin\*(Aq, \*(Aq/usr/X11/bin\*(Aq ] \& } .Ve .PP The grouping element (\f(CW\*(C`<searchpath>\*(C'\fR in the example) must not contain any attributes or elements other than the grouped element. .PP You can specify multiple 'grouping element' to 'grouped element' mappings in the same hashref. If this option is combined with \f(CW\*(C`KeyAttr\*(C'\fR, the array folding will occur first and then the grouped element names will be eliminated. .PP \&\f(CW\*(C`XMLout\*(C'\fR 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 'grouping tag' name occurs \- you probably don't want to use the same name for elements as well as attributes. .SS "Handler => object_ref \fI# out \- \s-1SAX\s0 only\fP" .IX Subsection "Handler => object_ref # out - SAX only" Use the 'Handler' option to have \f(CW\*(C`XMLout()\*(C'\fR generate \s-1SAX\s0 events rather than returning a string of \s-1XML. \s0 For more details see \*(L"\s-1SAX SUPPORT\*(R"\s0 below. .PP Note: the current implementation of this option generates a string of \s-1XML\s0 and uses a \s-1SAX\s0 parser to translate it into \s-1SAX\s0 events. The normal encoding rules apply here \- your data must be \s-1UTF8\s0 encoded unless you specify an alternative encoding via the 'XMLDecl' option; and by the time the data reaches the handler object, it will be in \s-1UTF8\s0 form regardless of the encoding you supply. A future implementation of this option may generate the events directly. .SS "KeepRoot => 1 \fI# in+out \- handy\fP" .IX Subsection "KeepRoot => 1 # in+out - handy" In its attempt to return a data structure free of superfluous detail and unnecessary levels of indirection, \f(CW\*(C`XMLin()\*(C'\fR normally discards the root element name. Setting the 'KeepRoot' option to '1' will cause the root element name to be retained. So after executing this code: .PP .Vb 1 \& $config = XMLin(\*(Aq<config tempdir="/tmp" />\*(Aq, KeepRoot => 1) .Ve .PP You'll be able to reference the tempdir as \&\f(CW\*(C`$config\->{config}\->{tempdir}\*(C'\fR instead of the default \&\f(CW\*(C`$config\->{tempdir}\*(C'\fR. .PP Similarly, setting the 'KeepRoot' option to '1' will tell \f(CW\*(C`XMLout()\*(C'\fR that the data structure already contains a root element name and it is not necessary to add another. .SS "KeyAttr => [ list ] \fI# in+out \- important\fP" .IX Subsection "KeyAttr => [ list ] # in+out - important" This option controls the 'array folding' feature which translates nested elements from an array to a hash. It also controls the 'unfolding' of hashes to arrays. .PP For example, this \s-1XML:\s0 .PP .Vb 4 \& <opt> \& <user login="grep" fullname="Gary R Epstein" /> \& <user login="stty" fullname="Simon T Tyson" /> \& </opt> .Ve .PP would, by default, parse to this: .PP .Vb 12 \& { \& \*(Aquser\*(Aq => [ \& { \& \*(Aqlogin\*(Aq => \*(Aqgrep\*(Aq, \& \*(Aqfullname\*(Aq => \*(AqGary R Epstein\*(Aq \& }, \& { \& \*(Aqlogin\*(Aq => \*(Aqstty\*(Aq, \& \*(Aqfullname\*(Aq => \*(AqSimon T Tyson\*(Aq \& } \& ] \& } .Ve .PP If the option 'KeyAttr => \*(L"login\*(R"' were used to specify that the 'login' attribute is a key, the same \s-1XML\s0 would parse to: .PP .Vb 10 \& { \& \*(Aquser\*(Aq => { \& \*(Aqstty\*(Aq => { \& \*(Aqfullname\*(Aq => \*(AqSimon T Tyson\*(Aq \& }, \& \*(Aqgrep\*(Aq => { \& \*(Aqfullname\*(Aq => \*(AqGary R Epstein\*(Aq \& } \& } \& } .Ve .PP The key attribute names should be supplied in an arrayref if there is more than one. \f(CW\*(C`XMLin()\*(C'\fR will attempt to match attribute names in the order supplied. \f(CW\*(C`XMLout()\*(C'\fR will use the first attribute name supplied when \&'unfolding' a hash into an array. .PP Note 1: The default value for 'KeyAttr' is ['name', 'key', 'id']. 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. .PP Note 2: If you wish to use this option, you should also enable the \&\f(CW\*(C`ForceArray\*(C'\fR option. Without 'ForceArray', 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). .SS "KeyAttr => { list } \fI# in+out \- important\fP" .IX Subsection "KeyAttr => { list } # in+out - important" 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 'KeyAttr => { package => 'id' } will cause any package elements to be folded on the 'id' attribute. No other elements which have an 'id' attribute will be folded at all. .PP Note: \f(CW\*(C`XMLin()\*(C'\fR will generate a warning (or a fatal error in \*(L"\s-1STRICT MODE\*(R"\s0) if this syntax is used and an element which does not have the specified key attribute is encountered (eg: a 'package' element without an 'id' attribute, to use the example above). Warnings can be suppressed with the lexical \&\f(CW\*(C`no warnings;\*(C'\fR pragma or \f(CW\*(C`no warnings \*(AqXML::Simple\*(Aq;\*(C'\fR. .PP Two further variations are made possible by prefixing a '+' or a '\-' character to the attribute name: .PP The option 'KeyAttr => { user => \*(L"+login\*(R" }' will cause this \s-1XML:\s0 .PP .Vb 4 \& <opt> \& <user login="grep" fullname="Gary R Epstein" /> \& <user login="stty" fullname="Simon T Tyson" /> \& </opt> .Ve .PP to parse to this data structure: .PP .Vb 12 \& { \& \*(Aquser\*(Aq => { \& \*(Aqstty\*(Aq => { \& \*(Aqfullname\*(Aq => \*(AqSimon T Tyson\*(Aq, \& \*(Aqlogin\*(Aq => \*(Aqstty\*(Aq \& }, \& \*(Aqgrep\*(Aq => { \& \*(Aqfullname\*(Aq => \*(AqGary R Epstein\*(Aq, \& \*(Aqlogin\*(Aq => \*(Aqgrep\*(Aq \& } \& } \& } .Ve .PP The '+' indicates that the value of the key attribute should be copied rather than moved to the folded hash key. .PP A '\-' prefix would produce this result: .PP .Vb 12 \& { \& \*(Aquser\*(Aq => { \& \*(Aqstty\*(Aq => { \& \*(Aqfullname\*(Aq => \*(AqSimon T Tyson\*(Aq, \& \*(Aq\-login\*(Aq => \*(Aqstty\*(Aq \& }, \& \*(Aqgrep\*(Aq => { \& \*(Aqfullname\*(Aq => \*(AqGary R Epstein\*(Aq, \& \*(Aq\-login\*(Aq => \*(Aqgrep\*(Aq \& } \& } \& } .Ve .PP As described earlier, \f(CW\*(C`XMLout\*(C'\fR will ignore hash keys starting with a '\-'. .SS "NoAttr => 1 \fI# in+out \- handy\fP" .IX Subsection "NoAttr => 1 # in+out - handy" When used with \f(CW\*(C`XMLout()\*(C'\fR, the generated \s-1XML\s0 will contain no attributes. All hash key/values will be represented as nested elements instead. .PP When used with \f(CW\*(C`XMLin()\*(C'\fR, any attributes in the \s-1XML\s0 will be ignored. .SS "NoEscape => 1 \fI# out \- seldom used\fP" .IX Subsection "NoEscape => 1 # out - seldom used" By default, \f(CW\*(C`XMLout()\*(C'\fR will translate the characters '<', '>', '&' and \&'"' to '<', '>', '&' and '"' respectively. Use this option to suppress escaping (presumably because you've already escaped the data in some more sophisticated manner). .SS "NoIndent => 1 \fI# out \- seldom used\fP" .IX Subsection "NoIndent => 1 # out - seldom used" Set this option to 1 to disable \f(CW\*(C`XMLout()\*(C'\fR's default 'pretty printing' mode. With this option enabled, the \s-1XML\s0 output will all be on one line (unless there are newlines in the data) \- this may be easier for downstream processing. .SS "NoSort => 1 \fI# out \- seldom used\fP" .IX Subsection "NoSort => 1 # out - seldom used" Newer versions of XML::Simple sort elements and attributes alphabetically (*), by default. Enable this option to suppress the sorting \- possibly for backwards compatibility. .PP * Actually, sorting is alphabetical but 'key' attribute or element names (as in \&'KeyAttr') sort first. Also, when a hash of hashes is 'unfolded', the elements are sorted alphabetically by the value of the key field. .SS "NormaliseSpace => 0 | 1 | 2 \fI# in \- handy\fP" .IX Subsection "NormaliseSpace => 0 | 1 | 2 # in - handy" This option controls how whitespace in text content is handled. Recognised values for the option are: .IP "\(bu" 4 0 = (default) whitespace is passed through unaltered (except of course for the normalisation of whitespace in attribute values which is mandated by the \s-1XML\s0 recommendation) .IP "\(bu" 4 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) .IP "\(bu" 4 2 = whitespace is normalised in all text content .PP Note: you can spell this option with a 'z' if that is more natural for you. .SS "NSExpand => 1 \fI# in+out handy \- \s-1SAX\s0 only\fP" .IX Subsection "NSExpand => 1 # in+out handy - SAX only" This option controls namespace expansion \- the translation of element and attribute names of the form 'prefix:name' to '{uri}name'. For example the element name 'xsl:template' might be expanded to: \&'{http://www.w3.org/1999/XSL/Transform}template'. .PP By default, \f(CW\*(C`XMLin()\*(C'\fR will return element names and attribute names exactly as they appear in the \s-1XML. \s0 Setting this option to 1 will cause all element and attribute names to be expanded to include their namespace prefix. .PP \&\fINote: You must be using a \s-1SAX\s0 parser for this option to work (ie: it does not work with XML::Parser)\fR. .PP This option also controls whether \f(CW\*(C`XMLout()\*(C'\fR performs the reverse translation from '{uri}name' back to 'prefix:name'. The default is no translation. If your data contains expanded names, you should set this option to 1 otherwise \&\f(CW\*(C`XMLout\*(C'\fR will emit \s-1XML\s0 which is not well formed. .PP \&\fINote: You must have the XML::NamespaceSupport module installed if you want \&\f(CI\*(C`XMLout()\*(C'\fI to translate URIs back to prefixes\fR. .SS "NumericEscape => 0 | 1 | 2 \fI# out \- handy\fP" .IX Subsection "NumericEscape => 0 | 1 | 2 # out - handy" Use this option to have 'high' (non-ASCII) characters in your Perl data structure converted to numeric entities (eg: €) in the \s-1XML\s0 output. Three levels are possible: .PP 0 \- default: no numeric escaping (\s-1OK\s0 if you're writing out \s-1UTF8\s0) .PP 1 \- only characters above 0xFF are escaped (ie: characters in the 0x80\-FF range are not escaped), possibly useful with \s-1ISO8859\-1\s0 output .PP 2 \- all characters above 0x7F are escaped (good for plain \s-1ASCII\s0 output) .SS "OutputFile => <file specifier> \fI# out \- handy\fP" .IX Subsection "OutputFile => <file specifier> # out - handy" The default behaviour of \f(CW\*(C`XMLout()\*(C'\fR is to return the \s-1XML\s0 as a string. If you wish to write the \s-1XML\s0 to a file, simply supply the filename using the \&'OutputFile' option. .PP This option also accepts an \s-1IO\s0 handle object \- especially useful in Perl 5.8.0 and later for output using an encoding other than \s-1UTF\-8,\s0 eg: .PP .Vb 2 \& open my $fh, \*(Aq>:encoding(iso\-8859\-1)\*(Aq, $path or die "open($path): $!"; \& XMLout($ref, OutputFile => $fh); .Ve .PP Note, XML::Simple does not require that the object you pass in to the OutputFile option inherits from IO::Handle \- it simply assumes the object supports a \f(CW\*(C`print\*(C'\fR method. .SS "ParserOpts => [ XML::Parser Options ] \fI# in \- don't use this\fP" .IX Subsection "ParserOpts => [ XML::Parser Options ] # in - don't use this" \&\fINote: 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's just plain wrong \- fix the \s-1XML\s0\fR. .PP This option allows you to pass parameters to the constructor of the underlying XML::Parser object (which of course assumes you're not using \s-1SAX\s0). .SS "RootName => 'string' \fI# out \- handy\fP" .IX Subsection "RootName => 'string' # out - handy" By default, when \f(CW\*(C`XMLout()\*(C'\fR generates \s-1XML,\s0 the root element will be named \&'opt'. This option allows you to specify an alternative name. .PP Specifying either undef or the empty string for the RootName option will produce \s-1XML\s0 with no root elements. In most cases the resulting \s-1XML\s0 fragment will not be 'well formed' and therefore could not be read back in by \f(CW\*(C`XMLin()\*(C'\fR. Nevertheless, the option has been found to be useful in certain circumstances. .SS "SearchPath => [ list ] \fI# in \- handy\fP" .IX Subsection "SearchPath => [ list ] # in - handy" If you pass \f(CW\*(C`XMLin()\*(C'\fR 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's home directory, then in a global directory such as /etc. .PP If a filename is provided to \f(CW\*(C`XMLin()\*(C'\fR but SearchPath is not defined, the file is assumed to be in the current directory. .PP If the first parameter to \f(CW\*(C`XMLin()\*(C'\fR is undefined, the default SearchPath will contain only the directory in which the script itself is located. Otherwise the default SearchPath will be empty. .SS "StrictMode => 1 | 0 \fI# in+out seldom used\fP" .IX Subsection "StrictMode => 1 | 0 # in+out seldom used" This option allows you to turn \*(L"\s-1STRICT MODE\*(R"\s0 on or off for a particular call, regardless of whether it was enabled at the time XML::Simple was loaded. .SS "SuppressEmpty => 1 | '' | undef \fI# in+out \- handy\fP" .IX Subsection "SuppressEmpty => 1 | '' | undef # in+out - handy" This option controls what \f(CW\*(C`XMLin()\*(C'\fR 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 'undef' 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. .PP The option also controls what \f(CW\*(C`XMLout()\*(C'\fR 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. .SS "ValueAttr => [ names ] \fI# in \- handy\fP" .IX Subsection "ValueAttr => [ names ] # in - handy" Use this option to deal elements which always have a single attribute and no content. Eg: .PP .Vb 4 \& <opt> \& <colour value="red" /> \& <size value="XXL" /> \& </opt> .Ve .PP Setting \f(CW\*(C`ValueAttr => [ \*(Aqvalue\*(Aq ]\*(C'\fR will cause the above \s-1XML\s0 to parse to: .PP .Vb 4 \& { \& colour => \*(Aqred\*(Aq, \& size => \*(AqXXL\*(Aq \& } .Ve .PP instead of this (the default): .PP .Vb 4 \& { \& colour => { value => \*(Aqred\*(Aq }, \& size => { value => \*(AqXXL\*(Aq } \& } .Ve .PP Note: This form of the ValueAttr option is not compatible with \f(CW\*(C`XMLout()\*(C'\fR \- since the attribute name is discarded at parse time, the original \s-1XML\s0 cannot be reconstructed. .SS "ValueAttr => { element => attribute, ... } \fI# in+out \- handy\fP" .IX Subsection "ValueAttr => { element => attribute, ... } # in+out - handy" 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 \s-1XML\s0 to be reconstructed by \f(CW\*(C`XMLout()\*(C'\fR. .PP Note: You probably don't want to use this option and the NoAttr option at the same time. .SS "Variables => { name => value } \fI# in \- handy\fP" .IX Subsection "Variables => { name => value } # in - handy" This option allows variables in the \s-1XML\s0 to be expanded when the file is read. (there is no facility for putting the variable names back if you regenerate \&\s-1XML\s0 using \f(CW\*(C`XMLout\*(C'\fR). .PP A 'variable' is any text of the form \f(CW\*(C`${name}\*(C'\fR which occurs in an attribute value or in the text content of an element. If 'name' matches a key in the supplied hashref, \f(CW\*(C`${name}\*(C'\fR 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: \f(CW\*(C`[\ew.]+\*(C'\fR (ie: only 'word' characters and dots are allowed). .SS "VarAttr => 'attr_name' \fI# in \- handy\fP" .IX Subsection "VarAttr => 'attr_name' # in - handy" In addition to the variables defined using \f(CW\*(C`Variables\*(C'\fR, this option allows variables to be defined in the \s-1XML. A\s0 variable definition consists of an element with an attribute called 'attr_name' (the value of the \f(CW\*(C`VarAttr\*(C'\fR 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 \f(CW\*(C`Variables\*(C'\fR option. For example: .PP .Vb 7 \& XMLin( \*(Aq<opt> \& <dir name="prefix">/usr/local/apache</dir> \& <dir name="exec_prefix">${prefix}</dir> \& <dir name="bindir">${exec_prefix}/bin</dir> \& </opt>\*(Aq, \& VarAttr => \*(Aqname\*(Aq, ContentKey => \*(Aq\-content\*(Aq \& ); .Ve .PP produces the following data structure: .PP .Vb 7 \& { \& dir => { \& prefix => \*(Aq/usr/local/apache\*(Aq, \& exec_prefix => \*(Aq/usr/local/apache\*(Aq, \& bindir => \*(Aq/usr/local/apache/bin\*(Aq, \& } \& } .Ve .SS "XMLDecl => 1 or XMLDecl => 'string' \fI# out \- handy\fP" .IX Subsection "XMLDecl => 1 or XMLDecl => 'string' # out - handy" If you want the output from \f(CW\*(C`XMLout()\*(C'\fR to start with the optional \s-1XML\s0 declaration, simply set the option to '1'. The default \s-1XML\s0 declaration is: .PP .Vb 1 \& <?xml version=\*(Aq1.0\*(Aq standalone=\*(Aqyes\*(Aq?> .Ve .PP 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. .SH "OPTIONAL OO INTERFACE" .IX Header "OPTIONAL OO INTERFACE" The procedural interface is both simple and convenient however there are a couple of reasons why you might prefer to use the object oriented (\s-1OO\s0) interface: .IP "\(bu" 4 to define a set of default values which should be used on all subsequent calls to \f(CW\*(C`XMLin()\*(C'\fR or \f(CW\*(C`XMLout()\*(C'\fR .IP "\(bu" 4 to override methods in \fBXML::Simple\fR to provide customised behaviour .PP The default values for the options described above are unlikely to suit everyone. The \s-1OO\s0 interface allows you to effectively override \fBXML::Simple\fR's defaults with your preferred values. It works like this: .PP First create an XML::Simple parser object with your preferred defaults: .PP .Vb 1 \& my $xs = XML::Simple\->new(ForceArray => 1, KeepRoot => 1); .Ve .PP then call \f(CW\*(C`XMLin()\*(C'\fR or \f(CW\*(C`XMLout()\*(C'\fR as a method of that object: .PP .Vb 2 \& my $ref = $xs\->XMLin($xml); \& my $xml = $xs\->XMLout($ref); .Ve .PP 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. .PP Note: when called as methods, the \f(CW\*(C`XMLin()\*(C'\fR and \f(CW\*(C`XMLout()\*(C'\fR routines may be called as \f(CW\*(C`xml_in()\*(C'\fR or \f(CW\*(C`xml_out()\*(C'\fR. The method names are aliased so the only difference is the aesthetics. .SS "Parsing Methods" .IX Subsection "Parsing Methods" You can explicitly call one of the following methods rather than rely on the \&\f(CW\*(C`xml_in()\*(C'\fR method automatically determining whether the target to be parsed is a string, a file or a filehandle: .IP "parse_string(text)" 4 .IX Item "parse_string(text)" Works exactly like the \f(CW\*(C`xml_in()\*(C'\fR method but assumes the first argument is a string of \s-1XML \s0(or a reference to a scalar containing a string of \s-1XML\s0). .IP "parse_file(filename)" 4 .IX Item "parse_file(filename)" Works exactly like the \f(CW\*(C`xml_in()\*(C'\fR method but assumes the first argument is the name of a file containing \s-1XML.\s0 .IP "parse_fh(file_handle)" 4 .IX Item "parse_fh(file_handle)" Works exactly like the \f(CW\*(C`xml_in()\*(C'\fR method but assumes the first argument is a filehandle which can be read to get \s-1XML.\s0 .SS "Hook Methods" .IX Subsection "Hook Methods" You can make your own class which inherits from XML::Simple and overrides certain behaviours. The following methods may provide useful 'hooks' 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. .IP "handle_options(direction, name => value ...)" 4 .IX Item "handle_options(direction, name => value ...)" This method will be called when one of the parsing methods or the \f(CW\*(C`XMLout()\*(C'\fR method is called. The initial argument will be a string (either 'in' or 'out') and the remaining arguments will be name value pairs. .IP "\fIdefault_config_file()\fR" 4 .IX Item "default_config_file()" Calculates and returns the name of the file which should be parsed if no filename is passed to \f(CW\*(C`XMLin()\*(C'\fR (default: \f(CW\*(C`$0.xml\*(C'\fR). .IP "build_simple_tree(filename, string)" 4 .IX Item "build_simple_tree(filename, string)" Called from \f(CW\*(C`XMLin()\*(C'\fR or any of the parsing methods. Takes either a file name as the first argument or \f(CW\*(C`undef\*(C'\fR followed by a 'string' 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. .IP "\fInew_hashref()\fR" 4 .IX Item "new_hashref()" When the 'simple tree' data structure is being built, this method will be called to create any required anonymous hashrefs. .IP "sorted_keys(name, hashref)" 4 .IX Item "sorted_keys(name, hashref)" Called when \f(CW\*(C`XMLout()\*(C'\fR is translating a hashref to \s-1XML. \s0 This routine returns a list of hash keys in the order that the corresponding attributes/elements should appear in the output. .IP "escape_value(string)" 4 .IX Item "escape_value(string)" Called from \f(CW\*(C`XMLout()\*(C'\fR, takes a string and returns a copy of the string with \&\s-1XML\s0 character escaping rules applied. .IP "escape_attr(string)" 4 .IX Item "escape_attr(string)" Called from \f(CW\*(C`XMLout()\*(C'\fR, to handle attribute values. By default, just calls \&\f(CW\*(C`escape_value()\*(C'\fR, but you can override this method if you want attributes escaped differently than text content. .IP "numeric_escape(string)" 4 .IX Item "numeric_escape(string)" Called from \f(CW\*(C`escape_value()\*(C'\fR, to handle non-ASCII characters (depending on the value of the NumericEscape option). .IP "copy_hash(hashref, extra_key => value, ...)" 4 .IX Item "copy_hash(hashref, extra_key => value, ...)" Called from \f(CW\*(C`XMLout()\*(C'\fR, when 'unfolding' a hash of hashes into an array of hashes. You might wish to override this method if you're using tied hashes and don't want them to get untied. .SS "Cache Methods" .IX Subsection "Cache Methods" XML::Simple implements three caching schemes ('storable', 'memshare' and \&'memcopy'). You can implement a custom caching scheme by implementing two methods \- one for reading from the cache and one for writing to it. .PP For example, you might implement a new 'dbm' scheme that stores cached data structures using the \s-1MLDBM\s0 module. First, you would add a \&\f(CW\*(C`cache_read_dbm()\*(C'\fR 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 \f(CW\*(C`cache_read_dbm()\*(C'\fR method which accepted a data structure and a filename. .PP You would use this caching scheme by specifying the option: .PP .Vb 1 \& Cache => [ \*(Aqdbm\*(Aq ] .Ve .SH "STRICT MODE" .IX Header "STRICT MODE" If you import the \fBXML::Simple\fR routines like this: .PP .Vb 1 \& use XML::Simple qw(:strict); .Ve .PP the following common mistakes will be detected and treated as fatal errors .IP "\(bu" 4 Failing to explicitly set the \f(CW\*(C`KeyAttr\*(C'\fR option \- if you can't be bothered reading about this option, turn it off with: KeyAttr => [ ] .IP "\(bu" 4 Failing to explicitly set the \f(CW\*(C`ForceArray\*(C'\fR option \- if you can't be bothered reading about this option, set it to the safest mode with: ForceArray => 1 .IP "\(bu" 4 Setting ForceArray to an array, but failing to list all the elements from the KeyAttr hash. .IP "\(bu" 4 Data error \- KeyAttr is set to say { part => 'partnum' } but the \s-1XML\s0 contains one or more <part> elements without a 'partnum' attribute (or nested element). Note: if strict mode is not set but \f(CW\*(C`use warnings;\*(C'\fR is in force, this condition triggers a warning. .IP "\(bu" 4 Data error \- 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. .IP "\(bu" 4 Data error \- 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. .SH "SAX SUPPORT" .IX Header "SAX SUPPORT" From version 1.08_01, \fBXML::Simple\fR includes support for \s-1SAX \s0(the Simple \s-1API\s0 for \s-1XML\s0) \- specifically \s-1SAX2.\s0 .PP In a typical \s-1SAX\s0 application, an \s-1XML\s0 parser (or \s-1SAX \s0'driver') module generates \&\s-1SAX\s0 events (start of element, character data, end of element, etc) as it parses an \s-1XML\s0 document and a 'handler' module processes the events to extract the required data. This simple model allows for some interesting and powerful possibilities: .IP "\(bu" 4 Applications written to the \s-1SAX API\s0 can extract data from huge \s-1XML\s0 documents without the memory overheads of a \s-1DOM\s0 or tree \s-1API.\s0 .IP "\(bu" 4 The \s-1SAX API\s0 allows for plug and play interchange of parser modules without having to change your code to fit a new module's \s-1API. A\s0 number of \s-1SAX\s0 parsers are available with capabilities ranging from extreme portability to blazing performance. .IP "\(bu" 4 A \s-1SAX \s0'filter' 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 'pipelines'. .IP "\(bu" 4 One filter module might split a data stream to direct data to two or more downstream handlers. .IP "\(bu" 4 Generating \s-1SAX\s0 events is not the exclusive preserve of \s-1XML\s0 parsing modules. For example, a module might extract data from a relational database using \s-1DBI\s0 and pass it on to a \s-1SAX\s0 pipeline for filtering and formatting. .PP \&\fBXML::Simple\fR can operate at either end of a \s-1SAX\s0 pipeline. For example, you can take a data structure in the form of a hashref and pass it into a \&\s-1SAX\s0 pipeline using the 'Handler' option on \f(CW\*(C`XMLout()\*(C'\fR: .PP .Vb 3 \& use XML::Simple; \& use Some::SAX::Filter; \& use XML::SAX::Writer; \& \& my $ref = { \& .... # your data here \& }; \& \& my $writer = XML::SAX::Writer\->new(); \& my $filter = Some::SAX::Filter\->new(Handler => $writer); \& my $simple = XML::Simple\->new(Handler => $filter); \& $simple\->XMLout($ref); .Ve .PP You can also put \fBXML::Simple\fR at the opposite end of the pipeline to take advantage of the simple 'tree' data structure once the relevant data has been isolated through filtering: .PP .Vb 3 \& use XML::SAX; \& use Some::SAX::Filter; \& use XML::Simple; \& \& my $simple = XML::Simple\->new(ForceArray => 1, KeyAttr => [\*(Aqpartnum\*(Aq]); \& my $filter = Some::SAX::Filter\->new(Handler => $simple); \& my $parser = XML::SAX::ParserFactory\->parser(Handler => $filter); \& \& my $ref = $parser\->parse_uri(\*(Aqsome_huge_file.xml\*(Aq); \& \& print $ref\->{part}\->{\*(Aq555\-1234\*(Aq}; .Ve .PP 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 \s-1SAX\s0 events to a downstream handler: .PP .Vb 5 \& my $writer = XML::SAX::Writer\->new(); \& my $filter = XML::Simple\->new( \& DataHandler => sub { \& my $simple = shift; \& my $data = shift; \& \& # Modify $data here \& \& $simple\->XMLout($data, Handler => $writer); \& } \& ); \& my $parser = XML::SAX::ParserFactory\->parser(Handler => $filter); \& \& $parser\->parse_uri($filename); .Ve .PP \&\fINote: In this last example, the 'Handler' option was specified in the call to \&\f(CI\*(C`XMLout()\*(C'\fI but it could also have been specified in the constructor\fR. .SH "ENVIRONMENT" .IX Header "ENVIRONMENT" If you don't care which parser module \fBXML::Simple\fR uses then skip this section entirely (it looks more complicated than it really is). .PP \&\fBXML::Simple\fR will default to using a \fB\s-1SAX\s0\fR parser if one is available or \&\fBXML::Parser\fR if \s-1SAX\s0 is not available. .PP You can dictate which parser module is used by setting either the environment variable '\s-1XML_SIMPLE_PREFERRED_PARSER\s0' or the package variable \&\f(CW$XML::Simple::PREFERRED_PARSER\fR to contain the module name. The following rules are used: .IP "\(bu" 4 The package variable takes precedence over the environment variable if both are defined. To force \fBXML::Simple\fR to ignore the environment settings and use its default rules, you can set the package variable to an empty string. .IP "\(bu" 4 If the 'preferred parser' is set to the string 'XML::Parser', then XML::Parser will be used (or \f(CW\*(C`XMLin()\*(C'\fR will die if XML::Parser is not installed). .IP "\(bu" 4 If the 'preferred parser' is set to some other value, then it is assumed to be the name of a \s-1SAX\s0 parser module and is passed to XML::SAX::ParserFactory. If \s-1XML::SAX\s0 is not installed, or the requested parser module is not installed, then \f(CW\*(C`XMLin()\*(C'\fR will die. .IP "\(bu" 4 If the 'preferred parser' is not defined at all (the normal default state), an attempt will be made to load \s-1XML::SAX\s0. If \s-1XML::SAX\s0 is installed, then a parser module will be selected according to XML::SAX::ParserFactory's normal rules (which typically means the last \s-1SAX\s0 parser installed). .IP "\(bu" 4 if the 'preferred parser' is not defined and \fB\s-1XML::SAX\s0\fR is not installed, then \fBXML::Parser\fR will be used. \f(CW\*(C`XMLin()\*(C'\fR will die if XML::Parser is not installed. .PP Note: The \fB\s-1XML::SAX\s0\fR distribution includes an \s-1XML\s0 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. .SH "ERROR HANDLING" .IX Header "ERROR HANDLING" The \s-1XML\s0 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. \fBXML::Simple\fR will die with an appropriate message if it encounters a parsing error. .PP If dying is not appropriate for your application, you should arrange to call \&\f(CW\*(C`XMLin()\*(C'\fR in an eval block and look for errors in $@. eg: .PP .Vb 2 \& my $config = eval { XMLin() }; \& PopUpMessage($@) if($@); .Ve .PP Note, there is a common misconception that use of \fBeval\fR will significantly slow down a script. While that may be true when the code being eval'd is in a string, it is not true of code like the sample above. .SH "EXAMPLES" .IX Header "EXAMPLES" When \f(CW\*(C`XMLin()\*(C'\fR reads the following very simple piece of \s-1XML:\s0 .PP .Vb 1 \& <opt username="testuser" password="frodo"></opt> .Ve .PP it returns the following data structure: .PP .Vb 4 \& { \& \*(Aqusername\*(Aq => \*(Aqtestuser\*(Aq, \& \*(Aqpassword\*(Aq => \*(Aqfrodo\*(Aq \& } .Ve .PP The identical result could have been produced with this alternative \s-1XML:\s0 .PP .Vb 1 \& <opt username="testuser" password="frodo" /> .Ve .PP Or this (although see 'ForceArray' option for variations): .PP .Vb 4 \& <opt> \& <username>testuser</username> \& <password>frodo</password> \& </opt> .Ve .PP Repeated nested elements are represented as anonymous arrays: .PP .Vb 9 \& <opt> \& <person firstname="Joe" lastname="Smith"> \& <email>joe@smith.com</email> \& <email>jsmith@yahoo.com</email> \& </person> \& <person firstname="Bob" lastname="Smith"> \& <email>bob@smith.com</email> \& </person> \& </opt> \& \& { \& \*(Aqperson\*(Aq => [ \& { \& \*(Aqemail\*(Aq => [ \& \*(Aqjoe@smith.com\*(Aq, \& \*(Aqjsmith@yahoo.com\*(Aq \& ], \& \*(Aqfirstname\*(Aq => \*(AqJoe\*(Aq, \& \*(Aqlastname\*(Aq => \*(AqSmith\*(Aq \& }, \& { \& \*(Aqemail\*(Aq => \*(Aqbob@smith.com\*(Aq, \& \*(Aqfirstname\*(Aq => \*(AqBob\*(Aq, \& \*(Aqlastname\*(Aq => \*(AqSmith\*(Aq \& } \& ] \& } .Ve .PP 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 \f(CW\*(C`KeyAttr\*(C'\fR option): .PP .Vb 5 \& <opt> \& <person key="jsmith" firstname="Joe" lastname="Smith" /> \& <person key="tsmith" firstname="Tom" lastname="Smith" /> \& <person key="jbloggs" firstname="Joe" lastname="Bloggs" /> \& </opt> \& \& { \& \*(Aqperson\*(Aq => { \& \*(Aqjbloggs\*(Aq => { \& \*(Aqfirstname\*(Aq => \*(AqJoe\*(Aq, \& \*(Aqlastname\*(Aq => \*(AqBloggs\*(Aq \& }, \& \*(Aqtsmith\*(Aq => { \& \*(Aqfirstname\*(Aq => \*(AqTom\*(Aq, \& \*(Aqlastname\*(Aq => \*(AqSmith\*(Aq \& }, \& \*(Aqjsmith\*(Aq => { \& \*(Aqfirstname\*(Aq => \*(AqJoe\*(Aq, \& \*(Aqlastname\*(Aq => \*(AqSmith\*(Aq \& } \& } \& } .Ve .PP The <anon> tag can be used to form anonymous arrays: .PP .Vb 6 \& <opt> \& <head><anon>Col 1</anon><anon>Col 2</anon><anon>Col 3</anon></head> \& <data><anon>R1C1</anon><anon>R1C2</anon><anon>R1C3</anon></data> \& <data><anon>R2C1</anon><anon>R2C2</anon><anon>R2C3</anon></data> \& <data><anon>R3C1</anon><anon>R3C2</anon><anon>R3C3</anon></data> \& </opt> \& \& { \& \*(Aqhead\*(Aq => [ \& [ \*(AqCol 1\*(Aq, \*(AqCol 2\*(Aq, \*(AqCol 3\*(Aq ] \& ], \& \*(Aqdata\*(Aq => [ \& [ \*(AqR1C1\*(Aq, \*(AqR1C2\*(Aq, \*(AqR1C3\*(Aq ], \& [ \*(AqR2C1\*(Aq, \*(AqR2C2\*(Aq, \*(AqR2C3\*(Aq ], \& [ \*(AqR3C1\*(Aq, \*(AqR3C2\*(Aq, \*(AqR3C3\*(Aq ] \& ] \& } .Ve .PP Anonymous arrays can be nested to arbitrary levels and as a special case, if the surrounding tags for an \s-1XML\s0 document contain only an anonymous array the arrayref will be returned directly rather than the usual hashref: .PP .Vb 5 \& <opt> \& <anon><anon>Col 1</anon><anon>Col 2</anon></anon> \& <anon><anon>R1C1</anon><anon>R1C2</anon></anon> \& <anon><anon>R2C1</anon><anon>R2C2</anon></anon> \& </opt> \& \& [ \& [ \*(AqCol 1\*(Aq, \*(AqCol 2\*(Aq ], \& [ \*(AqR1C1\*(Aq, \*(AqR1C2\*(Aq ], \& [ \*(AqR2C1\*(Aq, \*(AqR2C2\*(Aq ] \& ] .Ve .PP 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 'content' key (see the \f(CW\*(C`ContentKey\*(C'\fR option): .PP .Vb 4 \& <opt> \& <one>first</one> \& <two attr="value">second</two> \& </opt> \& \& { \& \*(Aqone\*(Aq => \*(Aqfirst\*(Aq, \& \*(Aqtwo\*(Aq => { \*(Aqattr\*(Aq => \*(Aqvalue\*(Aq, \*(Aqcontent\*(Aq => \*(Aqsecond\*(Aq } \& } .Ve .PP Mixed content (elements which contain both text content and nested elements) will be not be represented in a useful way \- 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 \- check out the next section. .SH "WHERE TO FROM HERE?" .IX Header "WHERE TO FROM HERE?" \&\fBXML::Simple\fR is able to present a simple \s-1API\s0 because it makes some assumptions on your behalf. These include: .IP "\(bu" 4 You're not interested in text content consisting only of whitespace .IP "\(bu" 4 You don't mind that when things get slurped into a hash the order is lost .IP "\(bu" 4 You don't want fine-grained control of the formatting of generated \s-1XML\s0 .IP "\(bu" 4 You would never use a hash key that was not a legal \s-1XML\s0 element name .IP "\(bu" 4 You don't need help converting between different encodings .PP In a serious \s-1XML\s0 project, you'll 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 'Perl\-XML \s-1FAQ\s0' document which you can find at: <http://perl\-xml.sourceforge.net/faq/> .PP The advice in the \s-1FAQ\s0 boils down to a quick explanation of tree versus event based parsers and then recommends: .PP For event based parsing, use \s-1SAX \s0(do not set out to write any new code for XML::Parser's handler \s-1API \-\s0 it is obsolete). .PP For tree-based parsing, you could choose between the 'Perlish' approach of XML::Twig and more standards based \s-1DOM\s0 implementations \- preferably one with XPath support such as XML::LibXML. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBXML::Simple\fR requires either XML::Parser or \s-1XML::SAX\s0. .PP To generate documents with namespaces, XML::NamespaceSupport is required. .PP The optional caching functions require Storable. .PP Answers to Frequently Asked Questions about XML::Simple are bundled with this distribution as: XML::Simple::FAQ .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 1999\-2004 Grant McLean <grantm@cpan.org> .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.