http://www.perlmonks.org?node_id=354371

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

Is there any way to force XML::Simple to keep the spaces?

use strict; use XML::Simple; use Data::Dumper; my $Parser = new XML::Simple( forcearray => [qw(LOCALE)], suppressempty => '', ); my $data = $Parser->XMLin(<<'*END*'); <tag> <subtag> </subtag> <othertag> ahoj </othertag> </tag> *END* print Dumper($data);
prints
$VAR1 = { 'othertag' => ' ahoj ', 'subtag' => '' };
I need the $data->{subtag} eq ' ', not ''.

I tried to encode the space as &#32;, but that did not make any difference. I also tried all other values of the suppressempty option, but that did not help. P.S.: It's quite possible there will be several spaces in the tag, I need them all!

Update: I patched XML::Simple and added a new value for the suppressempty option that does what I need.

Update:Actually the patch is incomplete, I need to get empty strings, not undefs for the completely empty tags. So I've added another option. ' ' = keep the spaces, return '' for empty tags vs. '0' = keep the spaces, return undef for empty tags. I'll post the updated patch later. If anyone has a better idea what values to use, I'm all ears :-)

Jenda
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
   -- Rick Osborne