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

XML::Simple Module

Part:   1   2  3  4  5 

This chapter describes:

  • Introduction to XML::Simple module.
  • Example Perl programs to use XML::Simple options.
  • Example Perl program to modify the parsed XML hash.

If you need to know more about XML, please read my other book: "Herong's Notes on XML Technologies".

XML::Simple Methods

XML::Simple module is an easy API to read and write XML files. It offers two main methods: XMLin() and XMLout().

XMLin(str) - Method to parse the XML input into a hash, and return the reference of the hash. The XML input can be specified in 3 ways:

  • If the method is called with no parameter, the XML input is in script_name.xml, where script_name is the same name of the calling Perl script file.
  • If the method is called with a string parameter containing <tag>, the XML input is the string param.
  • If the method is called with a string parameter without any <tag>, the XML input is in the file with the string param as file name.

XMLout(ref) - Method to write the hash pointed by the specified reference into an XML string, and return XML string.

Here is a simple program to show you how to use XML:Simple:

#- XmlSimpleHello.pl
#- Copyright (c) 1999 by Dr. Herong Yang
#
   use XML::Simple;
   my $xs = new XML::Simple();
   my $ref = $xs->XMLin("<p>Hello world!</p>");
   my $xml = $xs->XMLout($ref);
   print $xml;
   exit;

Output:

<opt>Hello world!</opt>

It's interesting to see from the output that the <p> tag has been changed to <opt> during the read and write operations. This is because of the default setting of the option: keeproot. I will explain some of the important options later in this chapter.

XML::Simple Options

XML::Simple options can be specified in the new() method call:

   $xs = new XML::Simple(option1 => value, option2 => value, ...);

Commonly used options are:

1. keeproot => 1: Applies to both XMLin() and XMLout() to keep the root tag.

2. searchpath => list: Applies to XMLin() to specifies the directories to search for input XML files.

3. forcearray => 1: Applies to XMLin() to force the contents of all elements to be an array.

4. suppressempty => 1 or '': Applies to XMLin() to skip empty elements or to represent them as '' strings. The default behavior is to represent empty elements as references of empty hashes. The default behavior makes it hard to access empty elements in the parsed hash.

5. keyattr => list: Applies to XMLin() and XMLout() to name attributes, or sub-elements as keys to be used to promot the parent element from array to hash. Remember that there is default list: "name", "key", and "id".

(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