Using Perl With XML (part 2) - Building A Library (
Page 6 of 9 )
Using this information, it's pretty easy to re-create our first example using the DOM parser. Here's the XML data,
<?xml version="1.0"?>
<library>
<book>
<title>Dreamcatcher</title>
<author>Stephen King</author>
<genre>Horror</genre>
<pages>899</pages>
<price>23.99</price>
<rating>5</rating>
</book>
<book>
<title>Mystic River</title>
<author>Dennis Lehane</author>
<genre>Thriller</genre>
<pages>390</pages>
<price>17.49</price>
<rating>4</rating>
</book>
<book>
<title>The Lord Of The Rings</title>
<author>J. R. R. Tolkien</author>
<genre>Fantasy</genre>
<pages>3489</pages>
<price>10.99</price>
<rating>5</rating>
</book>
</library>
and here's the script which does all the work.
#!/usr/bin/perl
# XML file
$file = "library.xml";
# array of ratings
@ratings = ("Words fail me!", "Terrible", "Bad", "Indifferent", "Good",
"Excellent");
# include package
use XML::DOM;
# instantiate parser
$xp = new XML::DOM::Parser();
# parse and create tree
$doc = $xp->parsefile($file);
# set up HTML page
print "Content-Type: text/html\n\n";
print "<html><head></head><body>";
print "<h2>The Library</h2>";
print "<table border=1 cellspacing=1 cellpadding=5> <tr> <td
align=center>Title</td> <td align=center>Author</td> <td
align=center>Price</td> <td align=center>User Rating</td> </tr>";
# get root node
$root = $doc->getDocumentElement();
# get children
@books = $root->getChildNodes();
# iterate through book list
foreach $node (@books)
{
print "<tr>";
# if element node
if ($node->getNodeType() == 1)
{
# get children
# this is the "title", "author"... level
@children = $node->getChildNodes();
# iterate through child nodes
foreach $item (@children)
{
# check element name
if (lc($item->getNodeName) eq "title")
{
# print text node contents under this element
print "<td><i>" . $item->getFirstChild()->getData . "</i></td>";
}
elsif (lc($item->getNodeName) eq "author")
{
print "<td>" . $item->getFirstChild()->getData . "</td>";
}
elsif (lc($item->getNodeName) eq "price")
{
print "<td>\$" . $item->getFirstChild()->getData . "</td>";
}
elsif (lc($item->getNodeName) eq "rating")
{
$num = $item->getFirstChild()->getData;
print "<td>" . $ratings[$num] . "</td>";
}
}
}
print "</tr>";
}
print "</table></body></html>";
# end
This may appear complex, but it isn't really all that hard to understand. I've first obtained a reference to the root of the document tree, $root, and then to the children of that root node; these children are returned as a regular Perl array. I've then used a "foreach" loop to iterate through the array, navigate to the next level, and print the content found in the nodes, with appropriate formatting. The numerous "if" statements you see are needed to check the name of each node and then add appropriate HTML formatting to it.
As explained earlier, the data itself is treated as a child text node of the corresponding element node. Therefore, whenever I find an element node, I've used the node's getFirstChild() method to access the text node under it, and the getData() method to extract the data from that text node.
Here's what it looks like: