A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

XML parsing fun with Perl

  • XML::Simple - No XPath, just simple nested hashrefs. Good options for collapsing keys. Useful for configuration files.
  • XML::Twig - Designed to handle sub-trees of very large documents, fast.
  • XML::XPath - find() method produces a nodelist, which is just an arrayref (see XML::XPath::XMLParser). It's dumb to have to "re-parse" nodes in order to look further down. Maybe I'm missing something?
  • XML::TreeBuilder or HTML::TreeBuilder - No XPath, but a splendid look_down() method. Returns objects on which you can also call look_down().
  • XML::LibXML and XML::LibXML::XPathContext - The best.
    • But HTML source often requires tidying into XML
    • And there is fun to be has converting entities
use XML::LibXML;
use XML::LibXML::XPathContext;
$parser = XML::LibXML->new();
$xml = $parser->parse_string($self->raw_xml);
$xpath = XML::LibXML::XPathContext->new($xml->documentElement);
$xpath->registerNs('atom', 'http://www.w3.org/2005/Atom');
print $xpath->findvalue("/atom:feed/atom:title");


Nothing seems good for testing XML paths, tags and attributes.