use warnings; use XML::Twig; use XML::Simple; $localfile= "Test_1.xml"; my $field = "Nest1"; open my $fout1, '>', "testx.csv" or die "Could not open file!"; $twig = XML::Twig->new( twig_roots => { $field => 1, 'd1' => 1, 'd2'=> 1, }, twig_handlers => { 'DatatoParse' => \&node, 'DatatoParse//*' => \&node1 } ); $twig->parsefile($localfile); sub node { my($twig, $el) = @_; $twig->purge; } sub node1{ print $fout1 "\n", if ($_->tag eq "d1"); print $fout1 $_->text, ",", unless ($_->has_children('#ELT')); print $fout1 "\n", if ($_->tag eq "elt"); }