On 8 December 2015 at 19:25, perl kamal <[email protected]> wrote:
> I am trying to parse the inner loop elements of the attached input xml
> elements.
> The below code doesn't retrieve the inner loop(<property>) elements if
> the properties tag contains more than one item. Will you please point
> the error and correct me.
> Please find the attached input xml file. Thanks.
A quick glance suggests you're getting bitten by one of the known
problems of XML::Simple: That its completely inconsistent.
2 seemingly identally strucutred XML files can be decoded completely
different to each other, so you need to have special cases everywhere
in your code *just in case* that happens.
Take for instance this simple code and its simple XML
use strict;
use warnings;
use utf8;
my $sample_a = <<"EOF";
<group>
<subgroup>
<item name="bruce" />
</subgroup>
</group>
EOF
my $sample_b = <<"EOF";
<group>
<subgroup>
<item name="mary" />
<item name="sue" />
</subgroup>
</group>
EOF
use XML::Simple;
use Data::Dump qw(pp);
my $sample_a_dec = XMLin($sample_a);
my $sample_b_dec = XMLin($sample_b);
pp {
a => $sample_a_dec,
b => $sample_b_dec,
};
It looks simple, it looks like a and be have similar enough data
structures, and you expect the pretty printed output to also be
similar, right? Right?
Nope!
Here, XML::Simple went a bit special snowflake.
{
a => { subgroup => { item => { name => "bruce" } } },
b => { subgroup => { item => { mary => {}, sue => {} } } },
}
At first glance you might overlook how these 2 entries are completely different.
One is a hash mapping:
"somevalue" => hash
The other is a has mapping:
"name" => some value
either it should be:
a => { subgroup => { item => { bruce => {} } } },
b => { subgroup => { item => { mary => {}, sue => {} } } },
or it should be
a => { subgroup => { item => [{ name => "bruce" }] }},
b => { subgroup => { item => [{ name => "mary" }, { name => "sue" }] } }
But XML::Simple gave you the worst of both worlds.
For this reason, XML::Simple is not recommended for real world work.
XML::Twig may be more what you're looking for.
Even its maintainer and author for 15 says "Hey, please don't use this" :)
--
Kent
KENTNL - https://metacpan.org/author/KENTNL
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/