Perl
  Home arrow Perl arrow Page 6 - Using Perl With XML (part 2)
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Using Perl With XML (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2002-02-01

    Table of Contents:
  • Using Perl With XML (part 2)
  • Meet Joe Cool
  • Parents And Their Children
  • What's In A Name?
  • Welcome To The Human Race
  • Building A Library
  • Anyone For Chicken?
  • Conclusions...
  • ...And Links

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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 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 Articles
    More By icarus, (c) Melonfire


     

       

    PERL ARTICLES

    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl
    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway