Herong's Tutorial Notes on Perl - Part B
Dr. Herong Yang, Version 4.11

XML::Simple Module

Part:   1  2  3  4  5  

(Continued from previous part...)

Notes about "keyattr":

  • Be careful, there is a default setting for "keyattr": [name, key, id].
  • If an attribute is found in the key list, the it's name will be removed, and it's value will be converted into a key in the parent hash.
  • Note that we have 'ARRAY(0x26426ec)' in the out. The reason is that we have a sub-element with the name "name", which is listed in "keyattr". When a sub-element is found in the key list, it will be promoted to the attribute level. But this will cause a problem on the value part. The content of the sub element is converted into array first, then this array is used as the value of this promoted attribute. Since this promoted attribute is converted into a key in the paremt hash, the array converted from the content will be used as the key. This is why you see 'ARRAY(0x26426ec)'.

Hash Modification Example - XmlSimpleHash.pl

The following example shows you hot to modify the resulting hash of the parsing operation. The important thing to remember when accessing the contents of the hash is that everything is parsed as array or hash. Hashes hold the tag names and attributes, and arrays hold their content.

#- XmlSimpleHash.pl
#- Copyright (c) 1999 by Dr. Herong Yang
#
   use XML::Simple;
   use Data::Dumper;
   my $xs = new XML::Simple(keeproot => 1,searchpath => ".",
      forcearray => 1, suppressempty => '');
   my $ref = $xs->XMLin("system.xml");
   my $xml = $xs->XMLout($ref);
   print "\nHash dump:\n";
   print Dumper($ref);
   print "\nXML output:\n";
   print $xml;
   $ref->{system}->[0]->{user}->[1]->{first_name}->[0] = "Bill";
   $ref->{system}->[0]->{user}->[1]->{email}->[0] = "bill\@wong.com";
   my $xml = $xs->XMLout($ref);
   print "\nUpdated XML output:\n";
   print $xml;
   exit;

Output:

Hash dump:
$VAR1 = {
          'system' => [
                        {
                          'content' => [
                                         '
 This is a testing system.
 ',
                                         '
 Needs to add more entries later.
'
                                       ],
                          'user' => [
                                      {
                                        'first_name' => [
                                                          'Mike'
                                                        ],
                                        'status' => 'active',
                                        'last_name' => [
                                                         'Lee'
                                                       ],
                                        'email' => [
                                                     'mike@lee.com'
                                                   ]
                                      },
                                      {
                                        'first_name' => [
                                                          ''
                                                        ],
                                        'last_name' => [
                                                         'Wong'
                                                       ],
                                        'content' => '
  Missing first name and email.
  ',
                                        'email' => [
                                                     ''
                                                   ]
                                      }
                                    ]
                        }
                      ]
        };

XML output:
<system>
  <content>
 This is a testing system.
 </content>
  <content>
 Needs to add more entries later.
</content>
  <user status="active">
    <first_name>Mike</first_name>
    <last_name>Lee</last_name>
    <email>mike@lee.com</email>
  </user>
  <user>
  Missing first name and email.
  <first_name></first_name>
    <last_name>Wong</last_name>
    <email></email>
  </user>
</system>

Updated XML output:
<system>
  <content>
 This is a testing system.
 </content>
  <content>
 Needs to add more entries later.
</content>
  <user status="active">
    <first_name>Mike</first_name>
    <last_name>Lee</last_name>
    <email>mike@lee.com</email>
  </user>
  <user>
  Missing first name and email.
  <first_name>Bill</first_name>
    <last_name>Wong</last_name>
    <email>bill@wong.com</email>
  </user>
</system>

Conclusions:

  • XML::Simple is really simple to use.
  • Element tags are parsed into hash keys, with hash values pointing to arrays containing the elements contents.
  • Attribute names are parsed into hash keys, with hash values being the attribute value strings.
  • Hashes and arrays are used as references in the resulting hash.
  • The order of child elements might be changed in the resulting hash.
  • Child elements of the same tag are grouped in to array.

Part:   1  2  3  4  5  

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Perl - Part B - XML::Simple Module