in reply to Creating a xml file in chunks

Hi vagabonding electron,

A while back I tried out XML::Writer, I believe it writes its output continually and only keeps a stack of tags that need to be closed, so I think it's probably worth a try for your purposes.

I did find its API a bit verbose and later stopped using it because of that. If your root tag is literally just "<ROOT>" with no attributes, just print it, as there's nothing to escape and therefore nothing that can go wrong. Of course if you've got complicated documents with attributes and namespaces etc. it's not that simple!

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: Creating a xml file in chunks
by vagabonding electron (Curate) on Jul 12, 2016 at 19:07 UTC

    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

      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