A simple example of how to read an XML document using Perl
This simple example shows how to read an XML document using Perl.
Suppose you have a file like this:
Code:
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="books.xsl" ?>
<books>
<book>
<title>Programming in XML</title>
<author>Marc Vince</author>
<pages>348</pages>
<category>Programming</category>
</book>
<book>
<title>Programming in C#</title>
<author>Loren Gallipyos</author>
<pages>234</pages>
<category>Programming</category>
</book>
<book>
<title>Android</title>
<author>Norman Bert</author>
<pages>419</pages>
<category>Operating Systems</category>
</book>
<book>
<title>Linux</title>
<author>Hurth Francy</author>
<pages>294</pages>
<category>Operating Systems</category>
</book>
<book>
<title>WordPress</title>
<author>Milena Zarch</author>
<pages>364</pages>
<category>CMS</category>
</book>
<book>
<title>Joomla!</title>
<author>Denise Patrics</author>
<pages>163</pages>
<category>CMS</category>
</book>
<book>
<title>jQuery</title>
<author>Teresa Curtis</author>
<pages>349</pages>
<category>Programming</category>
</book>
</books>
Code:
use XML::Simple;
use Data::Dumper;
$xml = new XML::Simple;
my $books = $xml->XMLin("books.xml");
print Dumper( $books );
The output from the example:
domenico@linux-4nve:~/public_html/perl> perl xml.pl
$VAR1 = {
'book' => [
{
'category' => 'Programming',
'author' => 'Marc Vince',
'title' => 'Programming in XML',
'pages' => '348'
},
{
'category' => 'Programming',
'author' => 'Loren Gallipyos',
'title' => 'Programming in C#',
'pages' => '234'
},
{
'category' => 'Operating Systems',
'author' => 'Norman Bert',
'title' => 'Android',
'pages' => '419'
},
{
'category' => 'Operating Systems',
'author' => 'Hurth Francy',
'title' => 'Linux',
'pages' => '294'
},
{
'category' => 'CMS',
'author' => 'Milena Zarch',
'title' => 'WordPress',
'pages' => '364'
},
{
'category' => 'CMS',
'author' => 'Denise Patrics',
'title' => 'Joomla!',
'pages' => '163'
},
{
'category' => 'Programming',
'author' => 'Teresa Curtis',
'title' => 'jQuery',
'pages' => '349'
}
]
};