XML::Generator::PerlData provides a simple way to generate SAX2 events from nested Perl data structures, while providing finer-grained control over the resulting document streams.

Processing comes in two flavors: Simple Style and Stream Style:

In a nutshell, 'simple style' is best used for those cases where you have a a single Perl data structure that you want to convert to XML as quickly and painlessly as possible. 'Stream style' is more useful for cases where you are receiving chunks of data (like from a DBI handle) and you want to process those chunks as they appear. See PROCESSING METHODS for more info about how each style works.

While basic usage of this module is designed to be simple and straightforward, there is a small host of options available to help ensure that the SAX event streams (and by extension the XML documents) that are created from the data structures you pass are in just the format that you want.

XML::Generator::PerlData is a SAX Driver/Generator. As such, it needs a SAX Handler or Filter class to forward its events to. The value for this option must be an instance of a SAX2-aware Handler or Filter.

rootname (optional)

Sets the name of the top-level (root) element. The default is 'document'.

defaultname (optional)

Sets the default name to be used for elements when no other logical name is available (think lists-of-lists). The default is 'default'.

keymap (optional)

Often, the names of the keys in a given hash do not map directly to the XML elements names that you want to appear in the resulting document. The option contains a set of keyname->element name mappings for the current process.

skipelements (optional)

Passed in as an array reference, this option sets the internal list of keynames that will be skipped over during processing. Note that any descendant structures belonging to those keys will also be skipped.

attrmap (optional)

Used to determine which 'children' of a given hash key/element-name will be forwarded as attributes of that element rather than as child elements.

(see CAVEATS for a discussion of the limitations of this method.)

namespaces (optional)

Sets the internal list of namespace/prefix pairs for the current process. It takes the form of a hash, where the keys are the URIs of the given namespace and the values are the associated prefix.

To set a default (unprefixed) namespace, set the prefix to '#default'.

namespacemap (optional)

Sets which elements in the result will be bound to which declared namespaces. It takes the form of a hash of key/value pairs where the keys are one of the declared namespace URIs that are relevant to the current process and the values are either single key/element names or an array reference of key/element names.

skiproot (optional)

When set to a defined value, this option blocks the generator from adding the top-level root element when parse() or parse_start() and parse_end() are called.

Do not use this option unless you absolutely sure you know what you are doing and why, since the resulting event stream will most likely produce non-well-formed XML.

bindattrs (optional)

When set to a defined value, this option tells the generator to bind attributes to the same namespace as element that contains them. By default attributes will be unbound and unprefixed.

processing_instructions (optional)

This option provides a way to include XML processing instructions events into the generated stream before the root element is emitted. The value of this key can be either a hash reference or an array reference of hash references. For example, when connected to XML::SAX::Writer:

Accepts: A reference to a Perl data structure. Optionally, a hash of config options.

Returns: [none]

The core method used during 'simple style' processing, this method accepts a reference to a Perl data structure and, based on the options passed, produces a stream of SAX events that can be used to transform that structure into XML. The optional second argument is a hash of config options identical to those detailed in the OPTIONS section of the the new() constructor description.

Starts the SAX event stream and (unless configured not to) fires the event the top-level root element. The optional argument is a hash of config options identical to those detailed in the OPTIONS section of the the new() constructor description.

The core method used during 'stream style' processing, this method accepts a reference to a Perl data structure and, based on the options passed, produces a stream of SAX events that can be used to transform that structure into XML.

All config options can be passed to calls to the new() constructor using the typical "hash of named properties" syntax. The methods below offer direct access to the individual options (or ways to add/remove the smaller definitions contained by those options).

Accepts: A hash (or hash reference) containing a series of keyname->elementname mappings or [none].

Returns: The current keymap hash (as a plain hash, or hash reference depending on caller context).

When called with a hash (hash reference) as its argument, this method sets/resets the entire internal keyname->elementname mappings definitions (where 'keyname' means the name of a given key in the hash and 'elementname' is the name used when firing SAX events for that key).

In addition to simple name->othername mappings, value of a keymap option can also a reference to a subroutine (or an anonymous sub). The keyname will be passed as the sole argument to this subroutine and the sub is expected to return the new element name. In the cases of nested arrayrefs, no keyname will be passed, but you can still generate the name from scratch.

Extending that idea, keymap will also accept a default mapping using the key '*' that will be applied to all elements that do have an explict mapping configured.

To add new mappings or remove existing ones without having to reset the whole list of mappings, see add_keymap() and delete_keymap() respectively.

If your are using "stream style" processing, this method should be used with caution since altering this mapping during processing may result in not-well-formed XML.

Accepts: A hash (or hash reference) containing a series of parent/child keyname pairs or [none].

Returns: The current charmap hash (as a plain hash, or hash reference depending on caller context).

When called with a hash (hash reference) as its argument, this method sets/resets the entire internal keyname/elementname->characters children mappings definitions (where 'keyname' means the name of a given key in the hash and 'characters children' is list containing the nested keynames that should be passed as the text children of the element named 'keyname' (instead of being processed as child elements or attributes).

To add new mappings or remove existing ones without having to reset the whole list of mappings, see add_charmap() and delete_charmap() respectively.

Deletes a list of parent-key -> child-key relationships from the instance-wide hash of "parent->nested names to pass as text children definitions. If you need to alter the list of child names (without deleting the parent key) use add_charmap() to reset the parent-key's definition.

Accepts: A hash (or hash reference) containing a series of parent/child keyname pairs or [none].

Returns: The current attrmap hash (as a plain hash, or hash reference depending on caller context).

When called with a hash (hash reference) as its argument, this method sets/resets the entire internal keyname/elementname->attr children mappings definitions (where 'keyname' means the name of a given key in the hash and 'attr children' is list containing the nested keynames that should be passed as attributes of the element named 'keyname' (instead of as child elements).

To add new mappings or remove existing ones without having to reset the whole list of mappings, see add_attrmap() and delete_attrmap() respectively.

Deletes a list of parent-key -> child-key relationships from the instance-wide hash of "parent->nested names to pass as attributes" definitions. If you need to alter the list of child names (without deleting the parent key) use add_attrmap() to reset the parent-key's definition.

Add a namespace URI/prefix pair to the instance-wide list of XML namespaces that will be used while processing. The reserved prefix '#default' can be used to set the default (unprefixed) namespace declaration for elements.

Accepts: A hash (or hash reference) containing a series of uri->key/element name mappings or [none].

Returns: The current namespacemap hash (as a plain hash, or hash reference depending on caller context).

When called with a hash (hash reference) as its argument, this method sets/resets the entire internal namespace URI->keyname/elementname mappings definitions (where 'keyname' means the name of a given key in the hash and 'namespace URI' is a declared namespace URI for the given process).

To add new mappings or remove existing ones without having to reset the whole list of mappings, see add_namespacemap() and delete_namespacemap() respectively.

If your are using "stream style" processing, this method should be used with caution since altering this mapping during processing may result in not-well-formed XML.

As a subclass of XML::SAX::Base, XML::Generator::PerlData allows you to call all of the SAX event methods directly to insert arbitrary events into the stream as needed. While its use in this way is probably a Bad Thing (and only relevant to "stream style" processing) it is good to know that such fine-grained access is there if you need it.

With that aside, there may be cases (again, using the "stream style") where you'll want to insert single elements into the output (wrapping each array in series of arrays in single 'record' elements, for example).

The following methods may be used to simplify this task by allowing you to pass in simple element name strings and have the result 'just work' without requiring an expert knowledge of the Perl SAX2 implementation or forcing you to keep track of things like namespace context.

Take care to ensure that every call to start_tag() has a corresponding call to end_tag() or your documents will not be well-formed.

In general, XML is based on the idea that every bit of data is going to have a corresponding name (Elements, Attributes, etc.). While this is not at all a Bad Thing, it means that some Perl data structures do not map cleanly onto an XML representation.

Consider:

my %hash = ( foo => ['one', 'two', 'three'] );

How do you represent that as XML? Is it three 'foo' elements, or is it a 'foo' parent element with 3 mystery children? XML::Generator::PerlData chooses the former. Or:

<foo>one</foo>
<foo>two</foo>
<foo>three</foo>

Now consider:

my @lol = ( ['one', 'two', 'three'], ['four', 'five', 'six'] );

In this case you wind up with a pile of elements named 'default'. You can work around this by doing $pd->add_keymap( default => ['list', 'of', 'names'] ) but that only works if you know how many entries are going to be in each nested list.

The practical implication here is that the current version of XML::Generator::PerlData favors data structures that are based on hashes of hashes for deeply nested structures (especally when using Simple Style processing) and some options like attrmap do not work for arrays at all. Future versions will address these issues if sanely possible.