XML::Struct - Represent XML as data structure preserving element order
version 0.06
use XML::Struct qw(readXML writeXML hashifyXML); my $struct = readXML( "input.xml" ); my $dom = writeXML( $struct ); ...
XML::Struct implements a mapping of "document-oriented" XML to Perl data structures. The mapping preserves element order but only XML elements, attributes, and text nodes (including CDATA-sections) are included. In short, an XML element is represented as array reference:
[ $name => \%attributes, \@children ]
To give an example, with XML::Struct::Reader, this XML document:
<root> <foo>text</foo> <bar key="value"> text <doz/> </bar> </root>
is transformed to this structure:
[ "root", { }, [ [ "foo", { }, "text" ], [ "bar", { key => "value" }, [ "text", [ "doz", { } ] ] ] ]
The reverse transformation can be applied with XML::Struct::Writer.
Key-value (aka "data-oriented") XML, as known from XML::Simple can be created with hashifyXML:
hashifyXML
{ foo => "text", bar => { key => "value", doz => {} } }
Both parsing and serializing are fully based on XML::LibXML, so performance is better than XML::Simple and similar to XML::LibXML::Simple.
The following functions can be exported on request:
Read an XML document with XML::Struct::Reader. The type of source (string, filename, URL, IO Handle...) is detected automatically.
Write an XML document with XML::Struct::Writer.
Transforms an XML element into a flattened hash, similar to what XML::Simple returns. Attributes and child elements are treated as hash keys with their content as value. Text elements without attributes are converted to text and empty elements without attributes are converted to empty hashes.
The option root works similar to KeepRoot in XML::Simple.
root
KeepRoot
Key attributes (KeyAttr in XML::Simple) and the options ForceArray are not supported (yet?).
KeyAttr
ForceArray
XML::Simple, XML::Twig, XML::Fast, XML::GenericJSON, XML::Structured, XML::Smart...
Jakob Voß
This software is copyright (c) 2013 by Jakob Voß.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install XML::Struct, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XML::Struct
CPAN shell
perl -MCPAN -e shell install XML::Struct
For more information on module installation, please visit the detailed CPAN module installation guide.