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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |