osprofi has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to extract xml data with the xml::parser module and to store them into an array of hashes for further processing - tag extraction works fine, but storing the tag values into the array of hashes is the issue.
May be you can assist me here and what I am doing wrong:
xml data file:
<Catalog> <Category> <Name>Arts & Entertainment</Name> <Site> <Id>...</Id> <Title>...</Title> </Site> <Site> <Id>...</Id> <Title>...</Title> </Site> </Category> <Catalog>
perl prg:
Thanks for assistance, Peter$parser = XML::Parser->new(Handlers => {Start => \&start, Char => \&char, End => \&end} ); sub start{ my ($p, $elt, %atts) = @_; if ($elt eq 'Site') { %site = (); #this is the hash per one site } if ($elt eq 'Id') { $tag_id = 1; } if ($elt eq 'Title') { $tag_title = 1; } } sub char { my ($p, $str) = @_; if ($tag_id eq 1) { $id = trim($str); } if ($tag_title eq 1) { $title = trim($str); } } sub end{ my ($p, $elt) = @_; if ($tag_id eq 1 and $elt eq 'Id') { $site{ $id } = $id; $tag_id = 0; } if ($tag_title eq 1 and $elt eq 'Title') { $site{ $title } = $title; $tag_title = 0; } if ($elt eq 'Site') { push(@cb, %site); print %site . "\n"; } } # if I want to process now the @cb then no data are inside.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: parse xml and store data in array of hashesh
by Anonymous Monk on Jul 22, 2011 at 07:04 UTC | |
by osprofi (Initiate) on Jul 23, 2011 at 07:52 UTC | |
by Anonymous Monk on Jul 23, 2011 at 08:44 UTC | |
|
Re: parse xml and store data in array of hashesh
by metaperl (Curate) on Jul 22, 2011 at 16:41 UTC | |
|
Re: parse xml and store data in array of hashesh
by Jenda (Abbot) on Jul 25, 2011 at 21:39 UTC |