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...)

"forcearray" Example - XmlSimpleArray.pl

The following program shows you how to use options, keeproot, searchpath, and forcearray:

#- XmlSimpleArray.pl
#- Copyright (c) 1999 by Dr. Herong Yang
#
   use XML::Simple;
   use Data::Dumper;
   my $xs = new XML::Simple(keeproot => 1,searchpath => ".");
   my $ref = $xs->XMLin("user.xml");
   my $xml = $xs->XMLout($ref);
   print "\nHash dump without 'forcearray => 1':\n";
   print Dumper($ref);
   print "\nXML output without 'forcearray => 1':\n";
   print $xml;
   my $xs = new XML::Simple(keeproot => 1,searchpath => ".",
      forcearray => 1,);
   my $ref = $xs->XMLin("user.xml");
   my $xml = $xs->XMLout($ref);
   print "\nHash dump with 'forcearray => 1':\n";
   print Dumper($ref);
   print "\nXML output with 'forcearray => 1':\n";
   print $xml;
   exit;

The input file, user.xml, has the following XML:

<?xml version="1.0"?>
<user status="active">
 <!-- This is not a real user. -->
 <first_name>Mike</first_name>
 <last_name>Lee</last_name>
</user>

Here is the output of the program:

Hash dump without 'forcearray => 1':
$VAR1 = {
          'user' => {
                      'first_name' => 'Mike',
                      'status' => 'active',
                      'last_name' => 'Lee'
                    }
        };

XML output without 'forcearray => 1':
<user first_name="Mike" status="active" last_name="Lee" />

Hash dump with 'forcearray => 1':
$VAR1 = {
          'user' => [
                      {
                        'first_name' => [
                                          'Mike'
                                        ],
                        'status' => 'active',
                        'last_name' => [
                                         'Lee'
                                       ]
                      }
                    ]
        };

XML output with 'forcearray => 1':
<user status="active">
  <first_name>Mike</first_name>
  <last_name>Lee</last_name>
</user>

A couple of the interesting things to note here:

  • Remember to specify the directory with "searchpath" where the XML input file is, even if it is the current directory.
  • The ?xml statement in the XML input was ignored.
  • "keeproot" keeped the root tag "user" correctly for us.
  • Without "forcearray => 1", child elements with only text contents were parsed as hash entries of keys and values, where the values are the text contents. But the attribute of the parent element was also parsed as a hash entry. So those child elements and the attribute can not be distinguished once parsed into the hash structure.
  • If you want to maintain the difference between attributes and child elements with only text contents, you should use the "forcearray => 1" option.
  • The order of hash entries did not match the order of elements in the XML input.

"suppressempty" Example - XmlSimpleEmpty.pl

The following program shows you how to use option, suppressempty:

#- XmlSimpleEmpty.pl
#- Copyright (c) 1999 by Dr. Herong Yang
#
   use XML::Simple;
   use Data::Dumper;
   my $xs = new XML::Simple(keeproot => 1,searchpath => ".",
      forcearray => 1,);
   my $ref = $xs->XMLin("system.xml");
   my $xml = $xs->XMLout($ref);
   print "\nHash dump without suppressempty => '':\n";
   print Dumper($ref);
   print "\nXML output without suppressempty => '':\n";
   print $xml;
   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 with suppressempty => '':\n";
   print Dumper($ref);
   print "\nXML output with suppressempty => '':\n";
   print $xml;
   exit;

The input file, system.xml, has the following XML:

<?xml version="1.0"?>
<system>
 This is a testing system.
 <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>
 Needs to add more entries later.
</system>

(Continued on next part...)

Part:   1  2   3  4  5 

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