Oct 312012
 
Article Perl

There are many perl modules in CPAN implementing different approaches to process  XML-formatted files.

The XML::Simple module reviewed in this post may be the best suited for the simplest cases, such as configuration files of small size, not making use of advanced characteristics of the XML format.

Example file in XML format

XML format is a way of representing in a text file a hierarchical tree of elements.

Each element is optionally assigned:

  • a set of attributes ((key, value) pairs)
  • either a value, of a set of subelements.

In the example below we can see a XML document having a root element named “customers”, with two subelements named “client”:

The client subelements, in turn, have subelements “name”, “address”, etc.

There are also attributes “version” and “timestamp” assigned to the “customers” element, and attributes “identifier” assigned to “client” elements.

Reading a XML document with XML::Simple

The XML::Simple module from CPAN reads a whole XML document into a hashref.

Example:

This code assigns a hashref to the “$data” variable.

Each key in the hastable identifies:

  • an attribute, if the value is a primitive data type
  • an array of subelements if the value is an arrayref

Each subelement in the array can be:

  • a simple value
  • a hashref of subelements of the next level in the hierarchy

The code below can be used to traverse the tree of hashrefs and arrayrefs:

 

The subroutine “print_element” is recursive: it calls itself on each subelement of the element being processed. If this code is executed to read the sample xml file above, the output is:

Writing a XML document with XML::Simple

The XML::Simple module also implements a method XMLout. This method receives as argument a hashref pointing to data using the same structure as the one returned by XMLin, and converts it into XML format:

Index of posts related to perl programming

 Posted by at 9:14 am

 Leave a Reply

(required)

(required)