use strict; use warnings; use XML::Twig; my $twig= new XML::Twig( twig_handlers => { Vehicle => \&vehicle } ); $twig->parsefile('in.xml'); exit; sub vehicle { my ($twig, $car) = @_; return if $car->first_child('Summary'); my $file = $car->first_child('AuctionID')->text() . '.csv'; open my $fh, '>', $file or die "can not open $file: $!"; print $fh "AuctionID,Ref,VehicleID,Manufacturer\n"; print $fh join ',' , $car->first_child('AuctionID' )->text() , $car->first_child('Ref' )->text() , $car->first_child('VehicleID' )->text() , $car->first_child('Manufacturer')->text() ; print $fh "\n"; close $fh; } #### AuctionID,Ref,VehicleID,Manufacturer 25019,1480714,156171,TOYOTA