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

In reply to Re^2: Anomalous double spaced output from XML::Twig by GrandFather
in thread Anomalous double spaced output from XML::Twig by GrandFather

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.