http://qs321.pair.com?node_id=1167642


in reply to Re: Creating a xml file in chunks
in thread Creating a xml file in chunks

Thank you very much haukex!

The API of XML::Writer appeared to be a bit too complex for me at first. However I tried it just now - and indeed it seems to write the output continually (which I checked with the commented "Hi" line below. The syntax was not very verbose either on the second look :-) A bit strange (to my taste) is the option NEWLINES which adds a newline before the closing delimiter, however it does make the output human readable.

I will test the module with my real data. Many thanks again!

#!/perl use strict; use warnings FATAL => qw(all); use Text::CSV_XS; use XML::Writer; my $csv_par = { binary => 1, auto_diag => 1, allow_whitespace => 1, sep_char => ';', eol => $/, quote_char => undef, }; my $csv = Text::CSV_XS->new($csv_par); my @header = @{$csv->getline(*DATA)}; my %rec; $csv->bind_columns(\@rec{@header}); my $writer = XML::Writer->new(NEWLINES => 1, ENCODING => 'UTF-8'); # stdout. $writer->xmlDecl(); # ("UTF-8") already mentioned above. $writer->startTag('ROOT'); while ( $csv->getline(*DATA) ) { $writer->startTag('alpha', 'name' => $rec{"alpha"}); for my $other( qw(beta gamma) ) { $writer->startTag($other, 'name' => $rec{$other}); $writer->endTag($other); } # print "\t\tHi!\n"; $writer->endTag('alpha'); } $writer->endTag('ROOT'); $writer->end(); __DATA__ alpha;beta;gamma q;2;3 w;9;8 e;1;2 r;6;7 t;5;9 y;3;1

Replies are listed 'Best First'.
Re^3: Creating a xml file in chunks
by haukex (Archbishop) on Jul 12, 2016 at 20:33 UTC

    Hi vagabonding electron,

    The syntax was not very verbose either on the second look :-)

    This is of course just my personal opinion and not a good reason to not use the module, but its method naming just bugged me... when I'm writing Java I don't mind reallyLongMethodNamesThatDocumentEverythingTheMethodDoes (that's what autocomplete is for), but when writing Perl I prefer Perlish APIs. Some examples: the method "characters" could be named "text" (and accept multiple arguments), "startTag" could be named "start", or s/dataElement/tag/. It might seem minor but when I tried it out I just found myself typing "startTag" too often and my code actually got longer using this "helper" module. But anyways, that's just my two cents, if it works for you then don't let me stop you :-)

    Regards,
    -- Hauke D