NAME
XML::Struct - Represent XML as data structure preserving element order
VERSION
version 0.10
SYNOPSIS
use XML::Struct qw(readXML writeXML simpleXML);
my $xml = readXML( "input.xml" );
# [ root => { xmlns => 'http://example.org/' }, [ '!', [x => {}, [42]] ] ]
my $doc = writeXML( $xml );
# <?xml version="1.0" encoding="UTF-8"?>
# <root xmlns="http://example.org/">!<x>42</x></root>
my $simple = simpleXML( $xml, root => 'record' );
# { record => { xmlns => 'http://example.org/', x => 42 } }
DESCRIPTION
XML::Struct implements a mapping between XML and Perl data structures. By default, the mapping preserves element order, so it also suits for "document-oriented" XML.
In short, an XML element is represented as array reference:
[ $name => \%attributes, \@children ]
If your XML documents don't contain relevant attributes, you can also choose this format:
[ $name => \@children ]
The module XML::Struct::Reader (or function readXML
) can be used to parse XML into this structure and the module XML::Struct::Writer (or function writeXML) does the reverse.
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", { } ]
]
]
]
XML::Struct also supports a simple key-value (aka "data-oriented") format, as used by XML::Simple. With option simple
(or function simpleXML
) the document given above woule be transformed to this structure:
{
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.
FUNCTIONS
The following functions can be exported on request:
readXML( $source [, %options ] )
Read an XML document with XML::Struct::Reader. The type of source (string, filename, URL, IO Handle...) is detected automatically.
writeXML( $xml [, %options ] )
Write an XML document with XML::Struct::Writer.
simpleXML( $element [, %options ] )
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.
Key attributes (KeyAttr
in XML::Simple) and the option ForceArray
are not supported yet.
SEE ALSO
XML::Simple, XML::Twig, XML::Fast, XML::GenericJSON, XML::Structured, XML::Smart...
AUTHOR
Jakob Voß
COPYRIGHT AND LICENSE
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.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 150:
Unterminated C<...> sequence