in reply to Re: Anomalous double spaced output from XML::Twig
in thread Anomalous double spaced output from XML::Twig

The issue is that the original line ends are retained, but that new lines are expanded to OS specific line endings. One work around for modest size files is to slurp the file then use parse.

This code demonstrates the parsing problem:

use warnings; use strict; use XML::Twig; my $str = <<"XML"; <?xml version="1.0" encoding="utf-8"?>\r <experiment title="Blood Pressure" version="1.2" softwareVersion="1.1" +>\r <sourceFiles>\r </sourceFiles>\r <dataFiles>\r <dataFile>cuff practice.dat</dataFile>\r </dataFiles>\r </experiment>\r XML # Troublesome code print "******* Troublesome version\n"; my @fileList = qw(first second); my $sub = bindArgs (\&insertFiles, \@fileList); my $twig = XML::Twig->new ( twig_roots => {'sourceFiles' => $sub,}, # Insert file list twig_print_outside_roots => 1, # Print the rest ); $twig->parse ($str); sub bindArgs { my ($sub, @args) = @_; return sub {$sub->(@args, @_);}; } sub insertFiles { my ($sourceFiles, $twig, $node) = @_; for (@$sourceFiles) { my $eFile = $twig->root->new (sourcefile => $_); # create the +element $eFile->paste (first_child => $node); } $node->print (); }

Prints:

******* Troublesome version <?xml version="1.0" encoding="utf-8"?> <experiment title="Blood Pressure" version="1.2" softwareVersion="1.1" +> <sourceFiles><sourcefile>second</sourcefile><sourcefile>first</sourcef +ile></sourceFiles> <dataFiles> <dataFile>cuff practice.dat</dataFile> </dataFiles> </experiment>

It would be nice if XML::Twig or XML::Parser collapsed various line ending sequences to a single \n character or alternatively XML::Twig should ignore non-signficant line endings.


DWIM is Perl's answer to Gödel