XML::FromArrayref - Output XML described by a Perl data structure
Version 1.02
use XML::FromArrayref; print XML [ html => [ head => [ title => 'My Web page' ] ], [ body => 'Hello' ] ];
This module exports an XML() function that lets you easily print valid XML without embedding it in your Perl code.
Takes a list of strings and arrayrefs describing XML content and returns the XML string. The strings are encoded; each arrayref represents an XML element, as follows:
[ $tag_name, $attributes, @content ]
evaluates to an XML tag name. If $tag_name is false then the whole element is replaced by its content.
If an arrayref's first element is another arrayref instead of an tag name, then the value of the first item of that array will be included in the XML string but will not be encoded. This lets you include text in the XML that has already been entity-encoded.
is an optional hashref defining the element's attributes. If an attribute's value is undefined then the attribute will not appear in the generated XML string. Attribute values will be encoded. If there isn't a hashref in the second spot in the element-definition list then the element won't have any attributes in the generated XML.
is another list of strings and arrayrefs, which will be used to generate the content of the element. If the content list is empty, then the element has no content and will be represented in the generated XML string by a single empty-element tag.
Recursively renders XML elements from arrayrefs.
Takes a list with an element name and an optional hashref defining the element's attributes, and returns just the opening tag of the element. This and end_tag() are useful in those occasions when you really want to print out XML piecewise procedurally, rather than building the whole page in memory.
Just takes an element name and returns the end tag for that element.
Takes a hash of XML element attributes and returns an encoded string for use in a tag.
This makes it easy to add a valid XML declaration to your document.
This makes it easy to add a valid doctype declaration to your document.
Note that I've formatted the output XML for clarity - the XML() function returns it all machine-readable and compact.
Strings are just encoded and printed, so
print XML 'Hi there, this & that';
would print
Hi there, this & that
If an element's name is an arrayref, its first item is printed without being encoded; this lets you include text that is already encoded by double-bracketing it:
print XML [ copyright => [[ '© Angel Networks™' ]] ];
<copyright>© Angel Networks™</copyright>
You can map any element over a list to iterate it, and by testing the value being mapped over can wrap some values in sub-elements:
print XML map [ number => [ $_ > 100 && large => $_ ] ], 4, 450, 12, 44, 74, 102;
<number>4<number> <number><large>450</large></number> <number>12</number> <number>44</number> <number>74</number> <number><b>102</b></number>
Similarly, by testing the value being mapped over in the attributes hash, you can set an attribute for only some values. Note that you have to explicitly return undef to skip the attribute since 0 is a valid value for an attribute.
print XML [ states => map [ state => { selected => $_ eq $c{state} || undef }, $_ ], @states ];
<states> <state>Alabama</state> <state selected="1">Alaska</state> <state>Arkansas</state> ... </state>
assuming $c{state} equalled 'Alaska'.
Sometimes you really don't want to build the whole document before printing it; you'd rather loop through some data and print an element at a time. The start_tag and end_tag functions will help you do this:
print start_tag( [ document => { columns => 3 } ] ); print end_tag( 'document' );
<document columns="3"> </document>
You can print an XML declaration with the XMLdecl() function.
print XMLdecl();
would print the default XML declaration
<?xml version="1.0" encoding="UTF-8"?>
but you can change the version and encoding by passing up to two arguments:
print XMLdecl('1.1', 'CP-1252');
<?xml version="1.1" encoding="CP-1252"?>
The doctype() function can be called without arguments to print a default doctype:
print doctype(); <!DOCTYPE XML>
or with one argument to set the root element name:
print doctype('html'); <!DOCTYPE html>
The second argument, if defined, is a URI; if no third argument is given, then it's printed as a private SYSTEM URI:
print doctype('transaction', 'http://example.com/transaction.dtd'); <!DOCTYPE transaction SYSTEM "http://example.com/transaction.dtd">
The third argument, if defined, is a public ID which will make the doctype public:
print doctype('HTML', 'http://www.w3.org/TR/html4/strict.dtd', '-//W3C//DTD HTML 4.01//EN'); <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Finally, if a fourth argument is given, it's a internal subset, which could contain markup declarations for entities, elements, &c.
print doctype('transaction', undef, undef, '<!ELEMENT description (#PCDATA)>' ); <!DOCTYPE transaction [ <!ELEMENT description (#PCDATA)> ]>
The XML 1.0 specification
Nic Wolff, <nic@angel.net>
Please report any bugs or feature requests through the web interface at https://github.com/nicwolff/XML-FromArrayref/issues. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc XML::FromArrayref
You can also look for information at:
This module on GitHub
https://github.com/nicwolff/XML-FromArrayref
GitHub request tracker (report bugs here)
https://github.com/nicwolff/XML-FromArrayref/issues
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/XML-FromArrayref
Search CPAN
http://search.cpan.org/dist/XML-FromArrayref/
Copyright 2013 Nic Wolff.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install XML::FromArrayref, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XML::FromArrayref
CPAN shell
perl -MCPAN -e shell install XML::FromArrayref
For more information on module installation, please visit the detailed CPAN module installation guide.