====== Parsing XML files in Perl using XML::Simple ======
The Perl [[http://search.cpan.org/~grantm/XML-Simple-1.08/Simple.pm|XML::Simple module]] provides an easy way for parsing XML files. Using the provided //XMLin// function, the whole XML file is converted into a hash table which can then be used to access the XML entries. The following article uses a very simple example to explain how to use the XML::Simple module. The example script converts a catalog of books stored in an XML file into a HTML table.
===== A simple XML parser using XML::Simple =====
As an example, let's consider the following xml file.
O'Brien, Tim
MSXML3: A Comprehensive Guide
Computer
36.95
2000-12-01
The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.
Knorr, Stefan
Creepy Crawlies
Horror
4.95
2000-12-06
An anthology of horror stories about roaches,
centipedes, scorpions and other insects.
Corets, Eva
The Sundered Grail
Fantasy
5.95
2001-09-10
The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.
The above xml file is a database that stores a catalog of books.
Using the XML::Simple module, we are going to implement a simple Perl script that parses the xml file and renders its contents as an HTML table.
This is the script :
use XML::Simple;
my $file = './file.xml';
# Here we create a new parser object
my $parser= XML::Simple->new();
# We tell the parser to parse the input file into the
# variable $doc.
my $doc = $parser->XMLin($file);
open (HTML, '>output.html');
printf HTML "\n";
printf HTML "\n";
printf HTML "\n";
printf HTML "\n";
printf HTML "| ";
printf HTML "Title | \n";
printf HTML "";
printf HTML "Author | \n";
printf HTML "";
printf HTML "Description | \n\n";
printf HTML "
\n";
# The list of books is parsed as a hash in the variable $doc
# We access the book entries using the hash keys as follows
foreach my $key (keys (%{$doc->{book}})) {
printf HTML "\n";
# For each book we display the title, author and a discription
printf HTML "| ".$doc->{book}->{$key}->{title}." | \n";
printf HTML "".$doc->{book}->{$key}->{author}." | \n";
printf HTML "".$doc->{book}->{$key}->{description}." | \n";
printf HTML "
\n";
}
printf HTML "
\n";
printf HTML "\n";
printf HTML "\n";
close (HTML);
The output is as follows :
| Title |
Author |
Description |
| MSXML3: A Comprehensive Guide |
O'Brien, Tim |
The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more. |
| Creepy Crawlies |
Knorr, Stefan |
An anthology of horror stories about roaches,
centipedes, scorpions and other insects. |
| The Sundered Grail |
Corets, Eva |
The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy. |
The HTML rendered table of our XML data would look as follows :
\\
| Title |
Author |
Description |
| MSXML3: A Comprehensive Guide |
O'Brien, Tim |
The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more. |
| Creepy Crawlies |
Knorr, Stefan |
An anthology of horror stories about roaches,
centipedes, scorpions and other insects. |
| The Sundered Grail |
Corets, Eva |
The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy. |
{{tag>coding howto}}
~~DISCUSSION~~