use warnings; use strict; use XML::Twig; my $source_file = 'datafile_towns.xml'; my $out_file = 'illytowns.xml'; my $twig = XML::Twig->new( twig_handlers => { town => \&town }, ); $twig->parsefile($source_file); open (my $fh_out, '>', $out_file) or die "unable to open '$out_file' for writing: $!"; sub town { my ($twig, $town) = @_; my $townx = $town->first_child('location')->first_child('mapx')->text; my $towny = $town->first_child('location')->first_child('mapy')->text; if (($townx > 556) and ($towny > -2502) and ($towny < -2214)) { print $town->first_child('towndata')->first_child_text('townname'); print "\n"; } $twig->print($fh_out); $twig->purge; }