Hi,

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 &amp; Entertainment</Name> <Site> <Id>...</Id> <Title>...</Title> </Site> <Site> <Id>...</Id> <Title>...</Title> </Site> </Category> <Catalog>

perl prg:

$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.
Thanks for assistance, Peter

In reply to parse xml and store data in array of hashesh by osprofi

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.