use XML::LibXML;
use XML::Fast;
use XML::Simple;
use XML::Bare;
use XML::Compile::Schema;
use Data::Dumper;
$XML::Simple::PREFERRED_PARSER='XML::Parser'; # Found this great tip from perlmonks
use Benchmark qw/cmpthese/;
$doc='XML String ';
my $schema = XML::Compile::Schema->new('./myschema.xsd');
my $reader = $schema->compile(READER => '{myns}mytype');
# I have done this outside to compile schema once but not sure if it works like that
cmpthese timethese -10, {
libxml => sub { XML::LibXML->new->parse_string($doc) },
xmlfast => sub { XML::Fast::xml2hash($doc) },
xmlbare => sub { XML::Bare->new(text => $doc)->parse },
xmlsimple => sub { XML::Simple->new(ForceArray => 0, KeyAttr => {})->XMLin($doc); },
xmlcompile => sub {my $hash = $reader->("$doc");},
};
####
Rate xmlcompile xmlsimple xmlbare xmlfast libxml
xmlcompile 51.5/s -- -66% -97% -97% -97%
xmlsimple 149/s 190% -- -91% -92% -92%
xmlbare 1651/s 3107% 1006% -- -11% -11%
xmlfast 1846/s 3487% 1137% 12% -- -0%
libxml 1846/s 3487% 1137% 12% 0% --
####
use XML::Hash::LX;
# Usage with XML::LibXML
my $doc = XML::LibXML->new->parse_string($xml);
my $xp = XML::LibXML::XPathContext->new($doc);
$xp->registerNs('rss', 'http://purl.org/rss/1.0/');
# then process xpath
for ($xp->findnodes('//rss:item')) {
# and convert to hash concrete nodes
my $item = xml2hash($_);
print Dumper+$item
}
####
Parse benchmark:
Rate Simple Hash Twig Hash::LX Bare
Simple 11.3/s -- -2% -16% -44% -97%
Hash 11.6/s 2% -- -14% -43% -97%
Twig 13.5/s 19% 16% -- -34% -96%
Hash::LX 20.3/s 79% 75% 51% -- -95%
Bare 370/s 3162% 3088% 2650% 1721% --