in reply to perl writing to xml file (xml::writer)

Build a hash of arrays (HoA) perhaps.

#!perl use strict; use warnings; use XML::Writer; use Text::CSV; my $csv = Text::CSV->new ( { binary => 1, sep_char => ',', eol => $\, } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); #input file my $input_file = 'in.csv'; open my $fh_in,'<',$input_file or die "cannot open $input_file : $!"; my %hash=(); while ( my $line = $csv->getline( $fh_in ) ){ my $name = $line->[0]; push @{$hash{$name}},$line; } close $fh_in; #output file my $output_file = "myfile.xml"; open my $fh_out,'>',$output_file or die "cannot open $output_file : $!"; my $writer = new XML::Writer( OUTPUT => $fh_out, DATA_MODE => 1, DATA_INDENT => 2 ); for my $name (sort keys %hash){ $writer->startTag("structure", "name"=>$name); for my $ar ( @{$hash{$name}} ){ $writer->startTag("value", "name" => $ar->[1], "start"=> $ar->[2], "type" => $ar->[3]); $writer->endTag("value"); } $writer->endTag("structure"); $writer->end(); } close $fh_out;
poj

Replies are listed 'Best First'.
Re^2: perl writing to xml file (xml::writer)
by Anonymous Monk on Feb 03, 2016 at 06:41 UTC
    thanks poj. I try to do it in hashes before, but not really familiar and got some error. That's why I switched back to array.