This section provides a tutorial example on how to use the 'suppressempty' option to control how empty XML elements should be parsed.
The "suppressempty" option applies to XMLin() to control how empty elements should be parsed.
The following program shows you how to use the "suppressempty" option:
#- XmlSimpleEmpty.pl
#- Copyright (c) 1999 by Dr. Herong Yang, http://www.herongyang.com/
#
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>
Here is the output of the program:
Hash dump without suppressempty => '':
$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 without suppressempty => '':
<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>
Hash dump with suppressempty => '':
$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 with suppressempty => '':
<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>
A couple of the interesting things to note here:
Text in mixed context was parsed into a hash entry with the key hard coded as "content".
Texts separated by child elements were parsed into a single hash entry with the value
as a reference to an array of multiple entries.
Child elements with the same tag name were parsed into a single hash entry
with the value as a reference to an array of multiple entries.
Child elements with the same tag name were grouped together, even if they were
separated by other child elements in the XML input. This will change the order of
child elements in the XML output.
Without "suppressempty => ''", empty elements were indeed parsed as empty hashes.
With "suppressempty => ''", empty elements were indeed parsed as empty strings.