use strict; #use this! use warnings; use XML::Twig; # use XML::Simple; NOOOO! # $localfile= "Test_1.xml"; used DATA instead my $field = "Nest1"; open my $fout1, '>', "testx.csv" or die "Could not open file!"; my $twig = XML::Twig->new( twig_roots => { $field => 1, 'd1' => 1, 'd2'=> 1, }, twig_handlers => { 'DatatoParse' => \&node, 'DatatoParse//*' => \&node1 } ); $/=''; ## added this $twig->parse(); #modified this 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"); } __DATA__ ....