rbi has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm using XML::Simple to save the content of some hashes uesd by a CGI Perl application. I'm on a XP OS ($^O = MSWin32). But the same application will need to run under Linux as well.

Basically, I have "sessions" with parameters acquired from HTML pages (textfields, filefields, checkboxes, ...) and other processes, that I manage through hashes. At the end of one session, I save these hashes with XMLout. Then I can read the information with XMLin to restore previous sessions.

Everything works fine with this module, until I save the content of a textfield that has multiple lines.

Say I insert in the textbox some data like this, with a carriage return from the keyboard at the end of each line:

1 3 station_a.txt 1 23 station_b.txt 1 23 station_c.txt ...
I get content of the textbox with something like:

$meteo{data} = $q->param('data');
then I save the hash with:
XMLout(\%meteo,outputfile=>$myroot.'/meteo.xml');

The meteo.xml file, viewed with vi will will be:

<opt data="1 3 station_a.txt^M 1 23 station_b.txt^M 1 23 station_c.txt^M " />
If I later read the file into the hash with:

%meteo = %{ XMLin($myroot.'/meteo.xml') };

and I initialise the textfield, then carriage return is lost and the data in the textfield are all on the same line:

1 3 station_a.txt 1 23 station_b.txt 1 23 station_c.txt ...

I've tried with

$meteo{data} =~ s/\015?\012/\n/g; # PAG. 623 of Programming Perl

but it does nothing. So I guess that XMLin removes both ^M and newline before filling $meteo{data}.

Does anybody can suggest me how to deal with this?

Thank you in advance.
Roberto

Replies are listed 'Best First'.
Re: XML::Simple and newlines
by Wonko the sane (Curate) on Apr 10, 2003 at 14:09 UTC
    Your problem is that your data is being stored as an attrib.

    Try the "noattr" option for XML::Simple.

    #!/usr/local/bin/perl5.6.0 -w use strict; use XML::Simple; my $xs = XML::Simple->new(); my %data; $data{text} = q{ line 1 text line 2 text line 3 text }; print $xs->XMLout(\%data, noattr => 1)

    Outputs:
    :!./t1.pl <opt> <text> line 1 text line 2 text line 3 text </text> </opt>
    The data will be preserved inside the tags, rather than as
    an attrib of the tag itself.
    Wonko
Re: XML::Simple and newlines
by grantm (Parson) on Apr 10, 2003 at 19:01 UTC

    The reply from Wonko the sane is correct. The reason you're seeing this effect is that the XML recommendation mandates some whitespace normalisation rules that must be applied by XML parsers when reading attributes. Unfortunately not all XML parser modules work exactly the same way, but the simple rule is that if you don't want your whitespace messed with, don't store it in an attribute.

Re: XML::Simple and newlines
by rbi (Monk) on Apr 11, 2003 at 10:08 UTC
    Thank you very much for your explainations and comments. All is working now.
    I don't know practically anything yet of XML, however XML::Simple is so simple :) and efficient that already fits my needs perfectly.
    Roberto