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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.