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

NAME

XML::Filter::Dispatcher::AsHashHandler - convert SAX stream in to simple, data-oriented structure

SYNOPSIS

    ## Ordinarily used via the XML::Filter::Dispatcher's as_data_struct()
    ## built-in extension function for XPath

DESCRIPTION

This SAX2 handler builds a simple hash from XML. Text from each element and attribute is stored in the hash with a key of a relative path from the root down to the current element.

The goal is to produce a usable structure as simply and quickly as possible; use XML::Simple for more sophisticated applications.

The resulting data structure has one hash per element, one scalar per attribute, and one scalar per text string in each leaf element.

Warnings are emitted if any content other than whitespace is discarded.

The root element name is discarded.

If you are using namespaces, you must pass in the Namespaces option, otherwise not. Using namespaces without a Namespaces option or vice versa will not work.

Only start_document(), start_element(), characters(), end_element(), and end_document() are provided; so all comments, processing instructions etc., are discarded.

Examples

This XML:

    <root a="A">
        <a aa1="AA1" aa2="AA2">
            <b>B1</b>
            <b>B2</b>
        </a>
    </root>

with no options produces this structure:

    {
        '@a'     => 'A',
        'a/@aa1' => 'AA1',
        'a/@aa2' => 'AA2'
        'a/b'    => 'B2',
        ''       => '

        B1
        B2

', 'a' => ' B1 B2 ', }

Note 1: the name of the root element is discarded.

Note 2: the contents of the first <b> element are not directly accessible; like standard Perl hashes, the later initialization overwrites the former. Much data oriented XML does not have this issue.

This XML:

    <root
        xmlns="default-ns"
        xmlns:foo="foo-ns"
        a="A"
        foo:a="FOOA"
    >
        <a aa1="AA1" foo:aa1="AA2">
            <b>B1</b>
            <foo:b>B2</foo:b>
        </a>
    </root>

With these options:

    XML::Filter::Dispatcher::AsHashHandler->new(
        Namespaces => {
            ""    => "default-ns",
            "bar" => "foo-ns",
        },
        Rules => [
            "hash( root )" => sub { Dumper xvalue },
        ],
    )

produces this structure:

    {
        '@a'     => 'A',
        '@bar:a' => 'FOOA',
        'a/@aa1' => 'AA1',
        'a/@aa2' => 'AA2'
        'a/b'    => 'B2',
        ''       => '

        B1
        B2

', 'a' => ' B1 B2 ', }

Methods

new
   see above.
set_namespaces
    $h->set_namespaces(
        prefix1 => uri1,
    );

AUTHOR

    Barrie Slaymaker <barries@slaysys.com>

1 POD Error

The following errors were encountered while parsing the POD:

Around line 255:

You forgot a '=back' before '=head1'