| Processing XML with Perl | | Michel Rodriguez |
![]() Example: word count | | ![]() XML::Parser |
Example: extracting information from an XML document
Printing the price of wines with a rating of 9.
pyx wine.xml | perl ex1.pl |
if( /^\(rating/) { $in_rating= 1; } # entering the rating element
elsif( $in_rating && (/^-(\d+)/)) # the value in the rating element
{ $rating= $1; } # grab it!
elsif( /^\)rating/) { $in_rating= 0; } # leaving the rating element
elsif( /^\(price/) { $in_price= 1; } # entering the price element
elsif( $in_price && (/^-(\d+)/)) # the value ine the price element
{ $price= $1; # grab it
print "$price\n" if( $rating >= 9); # are we interested?
}
elsif( /^\)price/) { $in_price= 0; } # leaving the price element
|
![]() Example: word count | | ![]() XML::Parser |


