use strict; use warnings; use Data::Dumper; #use Data::Dumper::Concise; # i prefer Data::Dumper::Concise use XML::Twig; # individually process each element sub signal_handler { my ($data, $twig, $elem) = @_; # get the attributes of $elem () my $atts = $elem->atts(); if ($atts->{'sigid'} == 3464) { print "Found with sigid == 3464:\n",$elem->sprint(),"\n"; print "";; } # if you want to access the element in a way similar to XML::Simple: my $xml_simple_style_elem = $elem->simplify(); # check out the simplified structure: print Dumper($xml_simple_style_elem); print "";; # Example for Data Collection: my ($sigid, $id) = @{$atts}{qw/sigid id/}; if (defined $sigid and defined $id) { $data->{sigid_id_count}{$sigid}{$id}++; } # get all elements below which are called my @foo_subelements = $elem->descendants('foo'); $twig->purge; # explicitly free the memory }; sub main { my $fn = shift @ARGV; my %collected_data; my $twig = XML::Twig->new( twig_roots => { 'signal' => sub {signal_handler(\%collected_data, @_);}, }, ); eval { $twig->parsefile($fn); }; if ($@) { print STDERR "Failed to parse '$fn' ($@)\n"; } if (%collected_data) { print "I collected the following data:\n",Dumper(\%collected_data); } } main();