use strict; use warnings; use XML::Twig; my $xml = q{}; my $t = XML::Twig->new( twig_handlers => { timeLimit => sub { my $atts = $_->atts; foreach (keys %$atts) { /^(?:start|end)Time$/ && do {print "$_ => $atts->{$_}\n"; next;}; } }, }, ); $t->parse($xml); #### endTime => 2016-12-28T23:59:59 startTime => 2016-09-30T00:00:00 #### my @time_limits; my $t = XML::Twig->new( twig_handlers => { timeLimit => sub { my $atts = $_->atts; if (exists $atts->{startTime} && exists $atts->{endTime}) { push @time_limits, [$atts->{startTime}, $atts->{endTime}]; } }, }, ); $t->parse($xml); print "[$_->[0]], [$_->[1]]\n" foreach @time_limits; #### [2016-09-30T00:00:00], [2016-12-28T23:59:59]