XML::Rabbit is a Moose-based class construction toolkit you can use to make XPath-based XML extractors with very little code. Each attribute in your class created with the above helper function is linked to an XPath query that is executed on your XML document when you request the value. Creating object hierarchies that mimic the layout of the XML document is almost as easy as doing a search and replace on the XML DTD (if you have one).

You can return multiple values from the XML either as arrays or hashes, depending on how you need to work with the data from the XML document.

The example in the synopsis shows how to create a class hierarchy that enables easy retrival of certain information from an XHTML document.

Also notice that if you specify an xpath query that can return multiple XML elements, you need to specify a hash map (xml tag => class name) instead of just specifying the class name returned.

All the array and hash-returning attributes are tagged with the Array and Hash native traits, so it is quick and easy to specify additional delegations. All the has_xpath_value* helpers expect the value to be of a Str type constraint. Array and hash-returning attributes automatically set their isa to ArrayRef[Str] and HashRef[Str] respectively.

All the helper methods and their associated arguments are explained in XML::Rabbit::Sugar detail.

Be aware that if your XML document contains a default XML namespace (like XHTML does), you MUST specify it with add_xpath_namespace(), or else your xpath queries will not match anything. The XML document is not scanned for XML namespaces during initialization.

The trait applied to the attribute will wrap the type constraint union in an ArrayRef if the trait name is XPathObjectList and as a HashRef if the trait name is XPathObjectMap. As all the traits that end with List return array references, their isa must be an ArrayRef. The same is valid for the *Map traits, just that they return HashRef instead of ArrayRef.

The namespace prefix used in isa_map MUST be specified in the namespace_map. If a prefix is used in isa_map without a corresponding entry in namespace_map an exception will be thrown.

Be aware of the syntax of XPath when used with namespaces. You should almost always define namespace_map when dealing with XML that use namespaces. Namespaces explicitly declared in the XML are usable with the prefix specified in the XML (except if you use isa_map). Be aware that a prefix must usually be declared for the default namespace (xmlns=...) to be able to use it in XPath queries. See the example above (on XHTML) for details. See "findnodes" in XML::LibXML::Node for more information.

Because XML::Rabbit uses XML::LibXML's DOM parser it is limited to handling XML documents that can fit in available memory. Unfortunately there is no easy way around this, because XPath queries need to work on a tree model, and I am not aware of any way of doing that without keeping the document in memory. Luckily XML::LibXML's DOM implementation is written in C, so it should use much less memory than a pure Perl DOM parser.

The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :)