Just to add another option to an old thread for future reference. If you do not like to use global variables, you can change the rules slightly and make the parser return the data:
use strict; use XML::Rules; my $xr = XML::Rules->new( stripspaces => 15, rules => { '^ShippingChargesCollection' => 'skip', # skip the <ShippingCharge +sCollection>...</ShippingChargesCollection> TaxableLocation => sub { my ($tag, $attr) = @_; return if $attr->{locationType} ne 'state'; # ignore other loc +ation types return '@states' => $attr->{locationValue}; # push the state i +nto the data of the parent tag }, TaxableLocationsCollection => 'pass no content', # dissolve the Ta +xableLocationsCollection, ignore text content Seller => 'pass', # dissolve the <Seller> tag SellerInformation => sub { my ($tag, $attr) = @_; return $attr->{sellerIdFromProvider} => {name => $attr->{_cont +ent}, states => $attr->{states}}; # sellerIdFromProvider and _content comes from the dissolved < +Seller> # states is an array that comes from <TaxableLocation> # use the id as the hash key }, 'DataFeed' => sub { delete $_[1]->{recordCount}; return $_[1]} # i +gnore the recordCount attribute, return the data from SellerInformati +on }); my $data = $xr->parsefile($filename); use Data::Dumper; print Dumper $data;
Jenda
Enoch was right!
Enjoy the last years of Rome.
In reply to Re: Reading multi-level-tag XML file
by Jenda
in thread Reading multi-level-tag XML file
by CSharma
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |