A Tutorial for using LibXML from Perl

In my ongoing crusade to direct people away from XML::Simple and towards XML::LibXML, I've recently published a documentation project called "Perl XML::LibXML by Example". The primary target of this documentation is the desperate Perl hacker who might otherwise reach for XML::Simple without realising the awful horrors that module will inflict upon them. My hope is that by pointing people at this documentation we'll be able to help them solve their problems sooner and with less pain.

In helping people with their Perl and XML problems I've come to realise that in order to become proficient with XML::LibXML what people really need (and what I've aimed to provide) is:

  • Complete, working code samples that they can copy and adapt to their needs.
  • Line-by-line explanations of what that example code is doing and why.
  • An introduction to what XPath is, and a range of example XPath expressions from the trivial to the complex with explanations of how they work.
  • A primer on the Document Object Model that concentrates on the tiny subset of methods that most of us actually need.

The documentation is structured in such a way that there's one obvious place for an absolute beginner to start but multiple other entry points for people to dip into specific subjects such as: XPath expressions; the DOM; namespaces; and using libxml with HTML. Along the way it provides multiple links to the official documentation so people know where to go when they need more information.

On the subject of XPath expressions, I've also included an interactive "XPath Sandbox" which allows you to type in expressions and see which parts of the document they match. (Full disclosure: it doesn't actually use XML::LibXML at all - it runs entirely within the browser using Javascript).

So please take a look at the site and if you are answering questions about Perl and XML, please consider linking to it in order to set people on the right path.

The full source for the project is up on GitHub so you can raise issues or fork the repository and raise pull requests to make it even better.


The tutorial is fantastic, thanks for writing it. It's very clear and easy to understand.

Another suggestion is use an XML::LibXML document object with

Together with an XSD it makes it really easy to go from an XML string or file to a Perl data structure and vice-versa, which is what XML::Simple provides and I expect many users of that are looking for, rather than xpath queries for every node. It also has the advantage that it fixes most of the parsing problems inherent in the XML::Simple (especially with the default options), so potentially increasing the robustness of code by enforcing validation in the conversion process.

Leave a comment

About Grant McLean

user-pic Perl hacker since 1995. Coordinator of Wellington.pm in New Zealand.