.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "XML::Struct::Writer 3pm"
.TH XML::Struct::Writer 3pm "2019-02-21" "perl v5.28.1" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
XML::Struct::Writer \- Write XML data structures to XML streams
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\& use XML::Struct::Writer;
\&
\& # serialize
\& XML::Struct::Writer\->new(
\& to => \e*STDOUT,
\& attributes => 0,
\& pretty => 1,
\& )\->write( [
\& doc => [
\& [ name => [ "alice" ] ],
\& [ name => [ "bob" ] ],
\& ]
\& ] );
\&
\& #
\& #
\& # alice
\& # bob
\& #
\&
\& # create DOM
\& my $xml = XML::Struct::Writer\->new\->write( [
\& greet => { }, [
\& "Hello, ",
\& [ emph => { color => "blue" } , [ "World" ] ],
\& "!"
\& ]
\& ] );
\& $xml\->toFile("greet.xml");
\&
\& #
\& # Hello, World!
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
This module writes an \s-1XML\s0 document, given as XML::Struct data structure, as
stream of \*(L"\s-1SAX EVENTS\*(R"\s0. The default handler receives these events with
XML::LibXML::SAX::Builder to build a \s-1DOM\s0 tree which can then be used to
serialize the \s-1XML\s0 document as string. The writer can also be used to directly
serialize \s-1XML\s0 with XML::Struct::Writer::Stream.
.PP
XML::Struct provides the shortcut function \f(CW\*(C`writeXML\*(C'\fR to this module.
.PP
\&\s-1XML\s0 elements can be passed in any of these forms and its combinations:
.PP
.Vb 1
\& # MicroXML:
\&
\& [ $name => \e%attributes, \e@children ]
\&
\& [ $name => \e%attributes ]
\&
\& [ $name ]
\&
\& # lax MicroXML also:
\&
\& [ $name => \e@children ]
\&
\& # SimpleXML:
\&
\& { $name => \e@children, $name => $content, ... }
.Ve
.SH "CONFIGURATION"
.IX Header "CONFIGURATION"
A XML::Struct::Writer can be configured with the following options:
.IP "to" 4
.IX Item "to"
Filename, IO::Handle, string reference, or other kind of stream to directly
serialize \s-1XML\s0 to with XML::Struct::Writer::Stream. This option is ignored
if \f(CW\*(C`handler\*(C'\fR is explicitly set.
.IP "handler" 4
.IX Item "handler"
A \s-1SAX\s0 handler to send \*(L"\s-1SAX EVENTS\*(R"\s0 to. If neither this option nor \f(CW\*(C`to\*(C'\fR is
explicitly set, an instance of XML::LibXML::SAX::Builder is used to build a
\&\s-1DOM.\s0
.IP "attributes" 4
.IX Item "attributes"
Ignore \s-1XML\s0 attributes if set to false. Set to true by default.
.IP "xmldecl" 4
.IX Item "xmldecl"
Include \s-1XML\s0 declaration on serialization. Enabled by default.
.IP "encoding" 4
.IX Item "encoding"
An encoding (for handlers that support an explicit encoding). Set to \s-1UTF\-8\s0
by default.
.IP "version" 4
.IX Item "version"
The \s-1XML\s0 version. Set to \f(CW1.0\fR by default.
.IP "standalone" 4
.IX Item "standalone"
Add standalone flag in the \s-1XML\s0 declaration.
.IP "pretty" 4
.IX Item "pretty"
Pretty-print \s-1XML.\s0 Disabled by default.
.SH "METHODS"
.IX Header "METHODS"
.ie n .SS "write( $root [, $name ] ) == writeDocument( $root [, $name ] )"
.el .SS "write( \f(CW$root\fP [, \f(CW$name\fP ] ) == writeDocument( \f(CW$root\fP [, \f(CW$name\fP ] )"
.IX Subsection "write( $root [, $name ] ) == writeDocument( $root [, $name ] )"
Write an \s-1XML\s0 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 \f(CW\*(C`root\*(C'\fR. This method is basically equivalent to:
.PP
.Vb 6
\& $writer\->writeStart;
\& $writer\->writeElement(
\& $writer\->microXML($root, $name // \*(Aqroot\*(Aq)
\& );
\& $writer\->writeEnd;
\& $writer\->result if $writer\->can(\*(Aqresult\*(Aq);
.Ve
.PP
The remaining methods expect \s-1XML\s0 in MicroXML format only.
.ie n .SS "writeElement( $element [, @more_elements ] )"
.el .SS "writeElement( \f(CW$element\fP [, \f(CW@more_elements\fP ] )"
.IX Subsection "writeElement( $element [, @more_elements ] )"
Write one or more \s-1XML\s0 elements and their child elements to the handler.
.ie n .SS "writeStart( [ $root [, $name ] ] )"
.el .SS "writeStart( [ \f(CW$root\fP [, \f(CW$name\fP ] ] )"
.IX Subsection "writeStart( [ $root [, $name ] ] )"
Call the handler's \f(CW\*(C`start_document\*(C'\fR and \f(CW\*(C`xml_decl\*(C'\fR methods. An optional root
element can be passed, so \f(CW\*(C`$writer\->writeStart($root)\*(C'\fR is equivalent to:
.PP
.Vb 2
\& $writer\->writeStart;
\& $writer\->writeStartElement($root);
.Ve
.ie n .SS "writeStartElement( $element )"
.el .SS "writeStartElement( \f(CW$element\fP )"
.IX Subsection "writeStartElement( $element )"
Directly call the handler's \f(CW\*(C`start_element\*(C'\fR method.
.ie n .SS "writeEndElement( $element )"
.el .SS "writeEndElement( \f(CW$element\fP )"
.IX Subsection "writeEndElement( $element )"
Directly call the handler's \f(CW\*(C`end_element\*(C'\fR method.
.ie n .SS "writeCharacters( $string )"
.el .SS "writeCharacters( \f(CW$string\fP )"
.IX Subsection "writeCharacters( $string )"
Directy call the handler's \f(CW\*(C`characters\*(C'\fR method.
.ie n .SS "writeEnd( [ $root ] )"
.el .SS "writeEnd( [ \f(CW$root\fP ] )"
.IX Subsection "writeEnd( [ $root ] )"
Directly call the handler's \f(CW\*(C`end_document\*(C'\fR method. An optional root element
can be passed, so \f(CW\*(C`$writer\->writeEnd($root)\*(C'\fR is equivalent to:
.PP
.Vb 2
\& $writer\->writeEndElement($root);
\& $writer\->writeEnd;
.Ve
.ie n .SS "microXML( $element [, $name ] )"
.el .SS "microXML( \f(CW$element\fP [, \f(CW$name\fP ] )"
.IX Subsection "microXML( $element [, $name ] )"
Convert an \s-1XML\s0 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.
.SH "SAX EVENTS"
.IX Header "SAX EVENTS"
A \s-1SAX\s0 handler, set with option \f(CW\*(C`handler\*(C'\fR, is expected to implement the
following methods (two of them are optional):
.ie n .IP "xml_decl( { Version => $version, Encoding => $encoding } )" 4
.el .IP "xml_decl( { Version => \f(CW$version\fR, Encoding => \f(CW$encoding\fR } )" 4
.IX Item "xml_decl( { Version => $version, Encoding => $encoding } )"
Optionally called once at the start of an \s-1XML\s0 document, if the handler supports
this method.
.IP "\fBstart_document()\fR" 4
.IX Item "start_document()"
Called once at the start of an \s-1XML\s0 document.
.ie n .IP "start_element( { Name => $name, Attributes => \e%attributes } )" 4
.el .IP "start_element( { Name => \f(CW$name\fR, Attributes => \e%attributes } )" 4
.IX Item "start_element( { Name => $name, Attributes => %attributes } )"
Called at the start of an \s-1XML\s0 element to emit an \s-1XML\s0 start tag.
.ie n .IP "end_element( { Name => $name } )" 4
.el .IP "end_element( { Name => \f(CW$name\fR } )" 4
.IX Item "end_element( { Name => $name } )"
Called at the end of an \s-1XML\s0 element to emit an \s-1XML\s0 end tag.
.ie n .IP "characters( { Data => $characters } )" 4
.el .IP "characters( { Data => \f(CW$characters\fR } )" 4
.IX Item "characters( { Data => $characters } )"
Called for character data. Character entities and \s-1CDATA\s0 section are expanded to
strings.
.IP "\fBend_document()\fR" 4
.IX Item "end_document()"
Called once at the end of an \s-1XML\s0 document.
.IP "\fBresult()\fR" 4
.IX Item "result()"
Optionally called at the end of \f(CW\*(C`write\*(C'\fR/\f(CW\*(C`writeDocument\*(C'\fR to return a value
from this methods. Handlers do not need to implement this method.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
Using a streaming \s-1SAX\s0 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 \s-1SAX\s0 events include \s-1XML::STX\s0, XML::SAX::SimpleDispatcher, and
XML::SAX::Machines,