Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: The best way to handle different type of XML files (Why I don't think much of XML::Simple)

by ikegami (Patriarch)
on Nov 21, 2009 at 23:14 UTC ( [id://808628]=note: print w/replies, xml ) Need Help??


in reply to Re^3: The best way to handle different type of XML files
in thread The best way to handle different type of XML files

I don't believe it. Let's compare the parsers by extracting the Person elements from the following very common structure:

... <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.

  • Comment on Re^4: The best way to handle different type of XML files (Why I don't think much of XML::Simple)
  • Select or Download Code

Replies are listed 'Best First'.
Re^5: The best way to handle different type of XML files
by mpeever (Friar) on Nov 22, 2009 at 22:02 UTC
    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.

    You got me thinking, and I think you're probably right.

    I'm playing with it now...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://808628]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-18 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found