Processing XML with Perl | Michel Rodriguez |
XML::Parser Styles | XML::Simple |
Example: updating an XML document
Change the currency from $ to FF
#!/bin/perl -w use strict; use XML::Parser; my $RATE= 7.20; my $in_price; my $parser= new XML::Parser( Style => "Stream"); $parser->parsefile( "wine.xml"); sub StartTag { if( $_[1] eq "price") { $in_price= 1; # DANGEROUS! # print '<price currency="FF">'; $_{currency}= 'FF'; print join( ' ', "<$_[1]", map { "$_=\" " . $_{$_} . "\"";} keys %_) . ">"; } else { print $_; } } sub Text { if( $in_price) { $_ *= $RATE; $in_price= 0; } print $_; } |
XML::Parser Styles | XML::Simple |