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

NAME

XML::Struct - Represent XML as data structure preserving element order

VERSION

version 0.06

SYNOPSIS

    use XML::Struct qw(readXML writeXML hashifyXML);

    my $struct = readXML( "input.xml" );

    my $dom = writeXML( $struct );

    ...

DESCRIPTION

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:

    {
        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.

EXPORTED 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.

hashify( $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 options 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.