This queue is for tickets about the XML-Simple CPAN distribution.

Report information
The Basics
Id:
95865
Status:
rejected
Priority:
Low/Low
Queue:

People
Owner:
grantm [...] cpan.org
Requestors:
dwest1975 [...] ukr.net
Cc:
AdminCc:

BugTracker
Severity:
(no value)
Broken in:
(no value)
Fixed in:
(no value)



Subject: Incorrect XML is produced with empty nested arrays
Date: Thu, 22 May 2014 17:08:41 +0300
To: bug-XML-Simple@rt.cpan.org
From: Dean West <dwest1975@ukr.net>

Hi,

Recently I noticed, that XML::Simple generates incorrect XML with default options if data has empty nested array(s). Generated node with empty nested array(s) looks like it contains data whereas it actually does not. Here is a simple demonstration:

Show quoted text
> perl -w <<'EOF'
> use XML::Simple;
>
> my $data = {
>     user => [
>         {
>             login => 'bob',
>             password => 'qwerty',
>             options => [],
>         },
>         {
>             login => 'alice',
>             password => 'ytrewq',
>             options => [],
>         },
>     ],
> };
>
> print XMLout($data);
> EOF

It produces the following XML:

<opt>
  <user login="bob" password="qwerty">
  </user>
  <user login="alice" password="ytrewq">
  </user>
</opt>

As you can see content for each 'user' node here is new line and 2 spaces. It stands to reason, that there should be no content at all in this case.

I have prepared a small patch which fixes this issue:

--- a/lib/XML/Simple.pm
+++ b/lib/XML/Simple.pm
@@ -1564,8 +1564,8 @@ sub value_to_xml {
         }

         if(ref($value)  or  $self->{opt}->{noattr}) {
-          push @nested,
-            $self->value_to_xml($value, $key, "$indent  ");
+          $value = $self->value_to_xml($value, $key, "$indent  ");
+          push @nested, $value if($value ne '');
         }
         else {
           $value = $self->escape_value($value) unless($self->{opt}->{noescape});

With this patch the following XML is produced:

<opt>
  <user login="bob" password="qwerty" />
  <user login="alice" password="ytrewq" />
</opt>

Please consider including it in future version.

--
Dean

Apologies for the delay in reviewing this patch. You referred to XML::Simple producing this output: <user login="bob" password="qwerty"> </user> And said "it stands to reason, that there should be no content at all in this case.". i.e.: <user login="bob" password="qwerty" /> Whilst I can see an argument either way, XMLin() will discard the extra whitespace so it makes no material difference. If you need that level of control over the generated XML then XML::Simple is a very poor tool for the job. Although it is a minor change, the upside is also small so I'm choosing not to accept the patch due to the potential risk of breaking downstream code. Regards Grant


This service runs on Request Tracker, is sponsored by The Perl Foundation, and maintained by Best Practical Solutions.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.