| XML::Struct::Writer(3pm) | User Contributed Perl Documentation | XML::Struct::Writer(3pm) |
XML::Struct::Writer - Write XML data structures to XML streams
use XML::Struct::Writer;
# serialize
XML::Struct::Writer->new(
to => \*STDOUT,
attributes => 0,
pretty => 1,
)->write( [
doc => [
[ name => [ "alice" ] ],
[ name => [ "bob" ] ],
]
] );
# <?xml version="1.0" encoding="UTF-8"?>
# <doc>
# <name>alice</name>
# <name>bob</name>
# </doc>
# create DOM
my $xml = XML::Struct::Writer->new->write( [
greet => { }, [
"Hello, ",
[ emph => { color => "blue" } , [ "World" ] ],
"!"
]
] );
$xml->toFile("greet.xml");
# <?xml version="1.0" encoding="UTF-8"?>
# <greet>Hello, <emph color="blue">World</emph>!</greet>
This module writes an XML document, given as XML::Struct data structure, as stream of "SAX EVENTS". The default handler receives these events with XML::LibXML::SAX::Builder to build a DOM tree which can then be used to serialize the XML document as string. The writer can also be used to directly serialize XML with XML::Struct::Writer::Stream.
XML::Struct provides the shortcut function "writeXML" to this module.
XML elements can be passed in any of these forms and its combinations:
# MicroXML:
[ $name => \%attributes, \@children ]
[ $name => \%attributes ]
[ $name ]
# lax MicroXML also:
[ $name => \@children ]
# SimpleXML:
{ $name => \@children, $name => $content, ... }
A XML::Struct::Writer can be configured with the following options:
Write an XML document, given as array reference (lax MicroXML), hash reference (SimpleXML), or both mixed. If given as hash reference, the name of a root tag can be chosen or it is set to "root". This method is basically equivalent to:
$writer->writeStart;
$writer->writeElement(
$writer->microXML($root, $name // 'root')
);
$writer->writeEnd;
$writer->result if $writer->can('result');
The remaining methods expect XML in MicroXML format only.
Write one or more XML elements and their child elements to the handler.
Call the handler's "start_document" and "xml_decl" methods. An optional root element can be passed, so "$writer->writeStart($root)" is equivalent to:
$writer->writeStart;
$writer->writeStartElement($root);
Directly call the handler's "start_element" method.
Directly call the handler's "end_element" method.
Directy call the handler's "characters" method.
Directly call the handler's "end_document" method. An optional root element can be passed, so "$writer->writeEnd($root)" is equivalent to:
$writer->writeEndElement($root);
$writer->writeEnd;
Convert an XML element, given as array reference (lax MicroXML) or as hash reference (SimpleXML) to a list of MicroXML elements and optionally remove attributes. Does not affect child elements.
A SAX handler, set with option "handler", is expected to implement the following methods (two of them are optional):
Using a streaming SAX handler, such as XML::SAX::Writer, XML::Genx::SAXWriter, XML::Handler::YAWriter, and possibly XML::Writer should be more performant for serialization. Examples of other modules that receive SAX events include XML::STX, XML::SAX::SimpleDispatcher, and XML::SAX::Machines,
| 2019-02-21 | perl v5.28.1 |