It's ime for the obnoxious XML::Twig guy to step in I guess ;--)

XML::Twig is designed for that kind of situation, it will let you load a view of the document that includes only the root and the Hit_num elements:

my $twig= XML::Twig->new( twig_roots => { Hit_num => 1 } ); $twig->parsefile( $file); my @hit_nums= $twig->root->children; # do stuff with the Hit_num's

Alternatively you can handle each Hit_num during the parsing:

my $twig= XML::Twig->new( twig_roots => { Hit_num => \&hit_num } ); $twig->parsefile( $file); sub hit_num { my( $t, $hit_num)= @_; # do stuff with the hit_num # $t->purge will free the memory if you don't need # to keep the hit_num around }

If you need to pass additional info to the handler you can use a closure, as diotalevi showed:

my $twig= XML::Twig->new( twig_roots => { Hit_num => sub { hit_num( @_ +, $state_info); } } ); # ... sub hit_num { my( $t, $hit_num, $state_info)= @_;

BTW there was a pretty good article by Simon Cozens on perl.com a while ago that gives more details on closures: Achieving Closure.


In reply to Re: XML::Parser and objects by mirod
in thread XML::Parser and objects by Ineffectual

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.