XML::Filter::Dispatcher::AsHashHandler - convert SAX stream in to simple, data-oriented structure
## Ordinarily used via the XML::Filter::Dispatcher's as_data_struct() ## built-in extension function for XPath
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.
Namespaces
Only start_document(), start_element(), characters(), end_element(), and end_document() are provided; so all comments, processing instructions etc., are discarded.
start_document()
start_element()
characters()
end_element()
end_document()
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.
<b>
<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
see above.
$h->set_namespaces( prefix1 => uri1, );
Barrie Slaymaker <barries@slaysys.com>
1 POD Error
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head1'
To install XML::Filter::Dispatcher, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XML::Filter::Dispatcher
CPAN shell
perl -MCPAN -e shell install XML::Filter::Dispatcher
For more information on module installation, please visit the detailed CPAN module installation guide.