View on
MetaCPAN
search.cpan.org is shutting down
For details read Perl NOC. After June 25th this page will redirect to MetaCPAN.org
Rob N ★ > XML-Quick > XML::Quick

Download:
XML-Quick-0.07.tar.gz

Dependencies

Annotate this POD

Website

View/Report Bugs
Module Version: 0.07   Source  

NAME ^

XML::Quick - Generate XML from hashes (and other data)

SYNOPSIS ^

    use XML::Quick;

    $xml = xml($data);
    
    $xml = xml($data, { ... });

DESCRIPTION ^

This module generates XML from Perl data (typically a hash). It tries hard to produce something sane no matter what you pass it. It probably fails.

When you use this module, it will export the xml function into your namespace. This function does everything.

xml

The simplest thing you can do is call xml a basic string. It will be XML-escaped for you:

    xml('v&lue');

    # produces: v&lue

To create a simple tag, you'll need to pass a hash instead:

    xml({
          'tag' => 'value'
        });
    
    # produces: <tag>value</tag>

Of course you can have several tags in the same hash:

    xml({
          'tag' => 'value',
          'tag2' => 'value2'
        });
    
    # produces: <tag2>value2</tag2>
    #           <tag>value</tag>

Arrays will be turned into multiple tags with the same name:

    xml({
          'tag' => [
                     'one',
                     'two',
                     'three'
                   ]
        });
    
    # produces: <tag>one</tag>
    #           <tag>two</tag>
    #           <tag>three</tag>

Use nested hashes to produce nested tags:

    xml({
          'tag' => {
                     'subtag' => 'value'
                   }
        });
    
    # produces: <tag>
    #             <subtag>value</subtag>
    #           </tag>

A hash key with a value of undef or an empty hash or array will produce a "bare" tag:

    xml({
          'tag' => undef
        });

    # produces: <tag/>

Adding attributes to tags is slightly more involved. To add attributes to a tag, include its attributes in a hash stored in the _attrs key of the tag:

    xml({
          'tag' => {
                     '_attrs' => {
                                   'foo' => 'bar'
                                 }
                   }
        });

    # produces: <tag foo='bar'/>

Of course, you're probably going to want to include a value or other tags inside this tag. For a value, use the _cdata key:

    xml({
          'tag' => {
                     '_attrs' => {
                                   'foo' => 'bar'
                                 },
                     '_cdata' => 'value'
                   }
        });

    # produces: <tag foo='bar'>value</tag>

For nested tags, just include them like normal:

    xml({
          'tag' => {
                     '_attrs' => {
                                   'foo' => 'bar'
                                 },
                     'subtag' => 'value'
                   }
        });
    
    # produces: <tag foo='bar'>
    #             <subtag>subvalue</subtag>
    #           </tag>

If you wanted to, you could include both values and nested tags, but you almost certainly shouldn't. See "BUGS AND LIMITATIONS" for more details.

There are also a number of processing options available, which can be specified by passing a hash reference as a second argument to xml():

BUGS AND LIMITATIONS ^

Because Perl hash keys get randomised, there's really no guarantee the generated XML tags will be in the same order as they were when you put them in the hash. This generally won't be a problem as the vast majority of XML-based datatypes don't care about order. I don't recommend you use this module to create XML when order is important (eg XHTML, XSL, etc).

Things are even more hairy when including character data alongside tags via the cdata or _cdata options. The cdata options only really exist to allow attributes and values to be specified for a single tag. The rich support necessary to support multiple character data sections interspersed alongside tags is entirely outside the scope of what the module is designed for.

There are probably bugs. This kind of thing is an inexact science. Feedback welcome.

SUPPORT ^

Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker at https://github.com/robn/XML-Quick/issues. You will be notified automatically of any progress on your issue.

Source Code

This is open source software. The code repository is available for public review and contribution under the terms of the license.

https://github.com/robn/XML-Quick

  git clone https://github.com/robn/XML-Quick.git

AUTHOR ^

Robert Norris <rob@eatenbyagrue.org>

CONTRIBUTORS ^

COPYRIGHT AND LICENSE ^

This software is copyright (c) 2005-2006 Monash University, (c) 2008-2015 by Robert Norris.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

syntax highlighting: