With XML::Rabbit
it is possible to consume XML files easily. You define in a declarative way and with an XPath syntax what you are looking for in the XML and XML::Rabbit
will return objects according to the given definition.
Definition:
package Bookstore;
use XML::Rabbit::Root;
has_xpath_object_list books => './book' => 'Bookstore::Book';
finalize_class();
package Bookstore::Book;
use XML::Rabbit;
has_xpath_value bookid => './@id';
has_xpath_value author => './author';
has_xpath_value title => './title';
has_xpath_value genre => './genre';
has_xpath_value price => './price';
has_xpath_value publish_date => './publish_date';
has_xpath_value description => './description';
has_xpath_object purchase_data => './purchase_data' => 'Bookstore::Purchase';
finalize_class();
package Bookstore::Purchase;
use XML::Rabbit;
has_xpath_value price => './price';
has_xpath_value date => './date';
finalize_class();
XML Consumption:
use strict;
use warnings;
use utf8;
package Library;
use feature qw(say);
use Carp;
use autodie;
say "Showing data information";
my $bookstore = Bookstore->new( file => './sample.xml' );
foreach my $book( @{$bookstore->books} ) {
say "ID: " . $book->bookid;
say "Title: " . $book->title;
say "Author: " . $book->author, "\n";
}
Notes:
Please be careful with the following:
The first class has to be XML::Rabbit::Root
. It will place you inside the main tag of the XML document. In our case it will place us inside <catalog>
Nested classes which are optional. Those classes need to be accessed via a try/catch (or eval / $@
check) block. Optional fields will simply return null
. For example, for purchase_data
the loop would be:
foreach my $book( @{$bookstore->books} ) {
say "ID: " . $book->bookid;
say "Title: " . $book->title;
say "Author: " . $book->author;
try {
say "Purchase price: ". $book->purchase_data->price, "\n";
} catch {
say "No purchase price available\n";
}
}
sample.xml
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
<purchase_data>
<date>2001-12-21</date>
<price>20</price>
</purchase_data>
</book>
</catalog>