MooseX::DOM - Easily Create DOM Based Objects
package RSS; use Moose; use MooseX::DOM; dom_value 'version' => '@version'; dom_nodes 'items' => ( fetch => dom_fetchnodes( xpath => 'channel/item', filter => dom_to_class('RSS::Item') ) ); # or, easy way (just get some DOM nodes) # dom_nodes 'items' => 'channel/items'; # or, create your own way to fetch the nodes # dom_nodes 'items' => ( # fetch => sub { ... } # ); no Moose; no MooseX::DOM; package RSS::Item; use Moose; use MooseX::DOM; dom_value 'title'; dom_value 'description'; dom_value 'link'; no Moose; no MooseX::DOM; sub BUILDARGS { my $class = shift; my $args = {@_ == 1? (dom_root => $_[0]) : @_}; return $args; } package main; # parse_file() is automatically created for you. my $rss = RSS->parse_file('rss.xml'); foreach my $item ($rss->items) { print "item link = ", $item->link, "\n"; print "item title = ", $item->title, "\n"; }
MooseX::DOM is a tool that allows you to define classes that are based on XML DOM.
The following DSL is provided upon calling MooseX::DOM. When no MooseX::DOM is used, these functions are removed from your namespace.
MooseX::DOM
no MooseX::DOM
Declares that a method named $name should be built, using the given spec. Returns a list of nodes, or what the filter argument trasnlates them to.
If %spec is omitted, $name is taken to be the xpath to fetch.
Declares that a method named $name should be built, using the given spec. Returns the result of the fetch, whatever that may be.
Creates a closure that fetches some nodes
Creates a closure that transforms nodes to something else, typically an object.
The following methods are built onto your class automatically.
These methods allow you to parse a piece of XML, and build a MooseX::DOM object based on it.
Does a DOM XPath lookup. Returns a plain DOM object.
Does a DOM XPath lookup. Returns whatever value the XPath results to.
By default dom_to_class() gives your object a single DOM element to play with. This is a problem if your class is a MooseX::DOM object and it doesn't already handle single argument constructors. In such cases, a simple builtin BUILDARGS can be provided for you. Simply do
package MyObject; use Moose; use MooseX::DOM qw(BUILDARGS);
Which will install a default BUILDARGS method for your class.
Daisuke Maki <daisuke@endeworks.jp>
<daisuke@endeworks.jp>
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
To install MooseX::DOM, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::DOM
CPAN shell
perl -MCPAN -e shell install MooseX::DOM
For more information on module installation, please visit the detailed CPAN module installation guide.