my $twig = XML::Twig->new( twig_handlers => { 'Mughals' => sub { my ($twig, $el) = @_; $twig->purge; }, 'Mughals//*' => sub { my $a = $_->tag; if ($a eq "Book") { print $fout1 "\n"; print $fout2 "\n"; } #print $fout1 $_->tag, ", ", $_->text, unless ($_->has_children('#ELT')); print $fout2 $_->tag, ",", unless ($_->has_children('#ELT')); print $fout1 $_->text, ",", unless ($_->has_children('#ELT')); } } ); $twig->parsefile('book.xml');