Perl Tutorials - Herong's Tutorial Examples - v6.02, by Herong Yang
XmlSimpleHash.pl - XML Hash Example
This section provides a tutorial example on how to work with hashes resulted from the XML::Simple parsing operation, XMLin().
The following example shows you how 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) HerongYang.com. All Rights Reserved. # 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>
Table of Contents
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
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 Directories and Read File Names
File System Functions and Operations
Socket Communication Over the Internet
►XML::Simple Module - XML Parser and Generator
"forcearray" - Forcing Element Contents as Arrays
"suppressempty" - Parsing Empty Elements
"keyattr" - Namings Attributes as Keys
►XmlSimpleHash.pl - XML Hash Example
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