DESCRIPTION

The XML::Structured module provides a way to convert xml data into a predefined perl data structure and back to xml. Unlike with modules like XML::Simple it is an error if the xml data does not match the provided skeleton (the "dtd"). Another advantage is that the order of the attributes and elements is taken from the dtd when converting back to xml.

XMLin()

The XMLin() function takes the dtd and a string as arguments and returns a hash reference containing the data.

XMLinfile()

This function works like XMLin(), but takes a filename or a file descriptor glob as second argument.

XMLout()

XMLout() provides the reverse operation to XMLin(), it takes a dtd and a hash reference as arguments and returns an XML string.

The DTD

The dtd parameter specifies the structure of the allowed xml data. It consists of nested perl arrays.

simple attributes and elements

The very simple example for a dtd is:

$dtd = [ 'user' =>
'login',
'password',
];

This dtd will accept/create XML like:

<user login="foo" password="bar" />

XMLin doesn't care if "login" or "password" are attributes or elements, so

<user>
<login>foo</login>
<password>bar</password>
</user>

is also valid input (but doesn't get re-created by XMLout()).

multiple elements of the same name

If an element may appear multiple times, it must be declared as an array in the dtd:

$dtd = [ 'user' =>
'login',
[ 'favorite_fruits' ],
];

XMLin will create an array reference as value in this case, even if the xml data contains only one element. Valid XML looks like:

As attributes may not appear multiple times, XMLout will create elements for this case. Note also that all attributes must come before the first element, thus the first array in the dtd ends the attribute list. As an example, the following dtd

multiple sub-elements with the same name

As with simple elements, one can allow sub-elements to occur multiple times. XMLin() creates an array of hash references in this case. The dtd specification uses an array reference to an array for this case, for example: