"forcearray" - Forcing Element Contents as Arrays

This section provides a tutorial example on how to use the 'forcearray' option to parse XML element contents as arrays instead of hashes.

The "forcearray" applies to the XMLin() method to force the contents of all elements to be parsed as arrays instead of hashes.

The following program shows you how to use the "forcearray" option and other options like, "keeproot" and "searchpath:

#- XmlSimpleArray.pl
#- Copyright (c) HerongYang.com. All Rights Reserved.
#
   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:

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Image and Picture Processing

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

XML::Simple Module - XML Parser and Generator

 XMLin() and XMLout() Methods

 XML Parsing Options

"forcearray" - Forcing Element Contents as Arrays

 "suppressempty" - Parsing Empty Elements

 "keyattr" - Namings Attributes as Keys

 XmlSimpleHash.pl - XML Hash Example

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB