...
<Persons>
<Person>...</Person>
<Person>...</Person>
<Person>...</Person>
</Persons>
...
The Persons element is optional and the number of Person elements is variable.
-
XML::Simple, without specifying a schema:
my $persons = $parent->{Persons};
my @persons =
!$persons ? ()
: !$persons->{Person} ? ()
: !ref($persons->{Person}) ? $persons->{Person}
: @{ $persons->{Person} };
-
XML::Simple, specifying a schema via ForceArray, etc:
GroupTags => { Persons => 'Person' },
ForceArray => [qw( Person )],
my @persons = $parent->{Persons} ? @{ $parent->{Persons} } : ();
-
XML:::LibXML:
my @persons = $parent->findnodes('Persons/Person');
Did I pick an example that XML::Simple handles poorly? Let's do another extremely common example to demonstrate otherwise. Let's extract the person's country.
...
<Person>
...
<Country ...>...</Country>
...
</Person>
...
-
XML::Simple, with default settings:
my $country =
!defined($person->{Country}) ? undef
: !ref($person->{Country}) ? $person->{Country}
: $person->{Country}{content};
-
XML::Simple, with ForceContent => 1:
my $country = $person->{Country} && $person->{Country}{content}
-
XML:::LibXML:
my $country = $person->findvalue('Country');
XML::Simple code is insane without a schema. It's much simpler with, but it's still longer and messier than with XML::LibXML. And it takes a lot of up-front time time to create the schema and lots of headaches from making mistakes.
With XML::LibXML, I don't have to do any of that up-front extra work XML::Simple requires. so in addition to being a better production parser (simpler, 50x faster, etc), it's a better prototyping parser too.
|