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