in reply to XML::Simple isn't listening to SuppressEmpty
All of the comments have either been: "that option doesn't do it", or "use a different module". Nobody managed to suggest either a fix/change for XML::Simple, or a set of options I can use that causes XML::Simple to become 'self-consistent' when dealing with partial content.
So in the end, I wrote a small post-processing routine to normalize the output from XMLin(). I.e recurse through the structure and a) promote leafs that are empty hashes to empty values.
{ } to ''
and b) hashes that contain only the single 'content' entry into simply the value:
{ content => 'xxx' } to 'xxx'
Now I consistently get what I expected regardless of data content.
Using the following:$VAR1 = \{ 'obj' => [ { 'class' => 'myclass', 'set' => { 'key2' => '', 'key1' => 'a' } }, { 'class' => 'myclass', 'set' => { 'key2' => 'b', 'key1' => 'a' } } ], 'name' => 'me' };
sub reduce { my ($ref) = @_; if (ref($ref) eq 'REF') { # recurse into references... $$ref = reduce($$ref); } elsif (UNIVERSAL::isa($ref, 'HASH')) { my @keys = keys %$ref; if (scalar (@keys) == 0) { # empty hash becomes an empty string return ''; } elsif (scalar (@keys) == 1 && $keys[0] eq 'content') { $ref = $ref->{$keys[0]}; # 'content'-only becomes the value } else { foreach (keys %$ref) { # recurse into multi-element hashes $ref->{$_} = reduce($ref->{$_}); } } } elsif (UNIVERSAL::isa($ref, 'ARRAY')) { foreach (@$ref) { # recurse into arrays reduce($_); } } return $ref; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML::Simple isn't listening to SuppressEmpty
by Anonymous Monk on May 08, 2012 at 15:55 UTC |