The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

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.

DSL PROVIDED TO SETUP YOUR CLASS

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.

METHODS AUTOMATICALLY PROVIDED TO YOUR CLASS

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.

MooseX::DOM METHODS

build_dom_fetchnodes

build_dom_nodes

build_dom_nodes_accessor

build_dom_nodes_appender

build_dom_to_class

build_dom_value

export_keywords

unexport_keywords

unimport

DEFAULT BUILDARGS

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.

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