NAME
MooseX::DOM - Easily Create DOM Based Objects
SYNOPSIS
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";
}
DESCRIPTION
MooseX::DOM is a tool that allows you to define classes that are based on XML DOM.
PROVIDED DSL
The following DSL is provided upon calling MooseX::DOM
. When no MooseX::DOM
is used, these functions are removed from your namespace.
dom_nodes $name => %spec
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.
dom_value $name => %spec
Declares that a method named $name should be built, using the given spec. Returns the result of the fetch, whatever that may be.
If %spec is omitted, $name is taken to be the xpath to fetch.
dom_fetchnodes %spec
Creates a closure that fetches some nodes
dom_to_class %spec
Creates a closure that transforms nodes to something else, typically an object.
PROVIDED METHODS
The following methods are built onto your class automatically.
parse_file
parse_string
parse_fh
These methods allow you to parse a piece of XML, and build a MooseX::DOM object based on it.
dom_findnodes($xpath)
Does a DOM XPath lookup. Returns a plain DOM object.
dom_findvalue($xpath)
Does a DOM XPath lookup. Returns whatever value the XPath results to.
AUTHOR
Daisuke Maki <daisuke@endeworks.jp>
LICENSE
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