NAME

XML::Struct - Convert document-oriented XML to data structures, preserving element order

VERSION

version 0.03

SYNOPSIS

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

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

my $dom = writeXML( $struct );

...

DESCRIPTION

This module implements a mapping of document-oriented XML to Perl data structures. The mapping preserves element order but XML comments, processing-instructions, unparsed entities etc. are ignored, similar to XML::Simple. 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, can be created with hashifyXML:

{
    foo => "text",
    bar => {
        key => "value",
        doz => {}
    }
}

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 )

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.

Key attributes (KeyAttr in XML::Simple) and the options KeepRoot and ForceArray are not supported (yet?).

SEE ALSO

XML::Simple, 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.