in reply to Parse XML into CSV Files

Here is how I would approach this problem using XML::Twig.

This reads in the XML file, and for each 'Vehicle' element, calls a subroutine named 'vehicle'. This sub opens a new output file whose name is the value of 'AuctionID' (since that seems unique) with a '.csv' extension. It outputs a comma-separated header line, then a CSV line. I only showed 4 values, but you could easily extend the code to add more values, in the order you require. This could be something you can build upon.

The code creates 2 output files: 25019.csv and 25020.csv.

Note: the XML you posted is invalid because it is missing a closing 'Vehicle' tag.

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; }

Here are the contents of output file 25019.csv:

AuctionID,Ref,VehicleID,Manufacturer 25019,1480714,156171,TOYOTA

Since some of your data does contain commas, it might be prudent to also use a CSV-type CPAN module such as Text::CSV_XS.