Home arrow Perl Programming arrow Page 6 - Using Perl With XML (part 2)

Building A Library - Perl

They say there's more than one way to skin a cat - and that'stwice as true when you're a Perl developer. In this concluding article onXML parsing with Perl, find out how the XML::DOM package provides analternative technique for manipulating XML elements and attributes, andcompare the two approaches to see which one works best for you.

TABLE OF CONTENTS:
  1. Using Perl With XML (part 2)
  2. Meet Joe Cool
  3. Parents And Their Children
  4. What's In A Name?
  5. Welcome To The Human Race
  6. Building A Library
  7. Anyone For Chicken?
  8. Conclusions...
  9. ...And Links
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 9
February 01, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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 packageuse XML::DOM;# instantiate parser$xp = new XML::DOM::Parser();# parse and create tree$doc = $xp->parsefile($file);# set up HTML pageprint "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> <tdalign=center>Title</td> <td align=center>Author</td> <tdalign=center>Price</td> <td align=center>User Rating</td> </tr>";# get root node$root = $doc->getDocumentElement();# get children@books = $root->getChildNodes();# iterate through book listforeach $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:



 
 
>>> More Perl Programming Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: