.\" Automatically generated by Pod::Man 4.11 (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::Descent 3pm" .TH XML::Descent 3pm "2020-11-01" "perl v5.30.3" "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::Descent \- Recursive descent XML parsing .SH "VERSION" .IX Header "VERSION" This document describes XML::Descent version 1.04 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use XML::Descent; \& \& # Create parser \& my $p = XML::Descent\->new( { Input => \e$xml } ); \& \& # Setup handlers \& $p\->on( \& folder => sub { \& my ( $elem, $attr ) = @_; \& \& $p\->on( \& url => sub { \& my ( $elem, $attr ) = @_; \& my $link = { \& name => $attr\->{name}, \& url => $p\->text \& }; \& } \& ); \& \& my $folder = $p\->walk; \& $folder\->{name} = $attr\->{name}; \& } \& ); \& \& # Parse \& my $res = $p\->walk; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" The conventional models for parsing \s-1XML\s0 are either \s-1DOM\s0 (a data structure representing the entire document tree is created) or \s-1SAX\s0 (callbacks are issued for each element in the \s-1XML\s0). .PP \&\s-1XML\s0 grammar is recursive \- so it's nice to be able to write recursive parsers for it. XML::Descent allows such parsers to be created. .PP Typically a new XML::Descent is created and handlers are defined for elements we're interested in .PP .Vb 9 \& my $p = XML::Descent\->new( { Input => \e$xml } ); \& $p\->on( \& link => sub { \& my ( $elem, $attr ) = @_; \& print "Found link: ", $attr\->{url}, "\en"; \& $p\->walk; # recurse \& } \& ); \& $p\->walk; # parse .Ve .PP A handler provides a convenient lexical scope that lasts until the closing tag of the element that triggered the handler is reached. .PP When called at the top level the parsing methods walk, text and xml parse the whole \s-1XML\s0 document. When called recursively within a handler they parse the portion of the document nested inside node that triggered the handler. .PP New handlers may be defined within a handler and their scope will be limited to the \s-1XML\s0 inside the node that triggered the handler. .SH "INTERFACE" .IX Header "INTERFACE" .ie n .SS """new( { options } )""" .el .SS "\f(CWnew( { options } )\fP" .IX Subsection "new( { options } )" Create a new XML::Descent. Options are supplied has a hash reference. The only option recognised directly by XML::Descent is \f(CW\*(C`Input\*(C'\fR which should be reference to the object that provides the \s-1XML\s0 source. Any value that can be passed as the first argument to \&\f(CW\*(C`XML::TokeParser\->new\*(C'\fR is allowed. .PP The remaining options are passed directly to \f(CW\*(C`XML::TokeParser\*(C'\fR. Consult that module's documentation for more details. .ie n .SS """walk""" .el .SS "\f(CWwalk\fP" .IX Subsection "walk" Parse part of the \s-1XML\s0 document tree triggering any handlers that correspond with elements it contains. When called recursively within a handler \f(CW\*(C`walk\*(C'\fR visits all the elements below the element that triggered the handler and then returns. .ie n .SS """on( [ element names ], handler )""" .el .SS "\f(CWon( [ element names ], handler )\fP" .IX Subsection "on( [ element names ], handler )" Register a handler to be called when the named element is encountered. Multiple element names may be supplied as an array reference. Multiple handlers may be registered with one call to \f(CW\*(C`on\*(C'\fR by supplying a number of element, handler pairs. .PP Calling \f(CW\*(C`on\*(C'\fR within a handler defines a nested local handler whose scope is limited to the containing element. Handlers are called with three arguments: the name of the element that triggered the handler, a hash of the element's attributes and a user defined context value \- see \&\f(CW\*(C`context\*(C'\fR for more about that. .PP For example: .PP .Vb 1 \& $p = XML::Descent\->new( { Input => \e$some_xml } ); \& \& # Global handler \- trigger anywhere an tag is found \& $p\->on( \& options => sub { \& my ( $elem, $attr, $ctx ) = @_; \& \& # Define a nested handler for elements that only \& # applies within the handler. \& $p\->on( \& name => sub { \& my ( $elem, $attr, $ctx ) = @_; \& # Get the inner text of the name element \& my $name = $p\->text; \& print "Name: $name\en"; \& } \& ); \& \& # Recursively walk elements inside triggering \& # any handlers \& $p\->walk; \& } \& ); \& \& # Start parsing \& $p\->walk; .Ve .PP A handler may call one of the parsing methods (\f(CW\*(C`walk\*(C'\fR, \f(CW\*(C`text\*(C'\fR, \f(CW\*(C`xml\*(C'\fR or \f(CW\*(C`get_token\*(C'\fR) to consume any nested \s-1XML\s0 before returning. If none of the parsing methods are called nested \s-1XML\s0 is automatically discarded so that the parser can properly move past the current element. .PP Nested handlers temporarily override another handler with the same name. A handler named '*' will trigger for all elements for which there is no explicit handler. A nested '*' handler hides all handlers defined in containing scopes. .PP As a shorthand you may specify a path to a nested element: .PP .Vb 3 \& $p\->on( \*(Aqa/b/c\*(Aq => sub { \& print "Woo!\en"; \& })\->walk; .Ve .PP That's equivalent to: .PP .Vb 7 \& $p\->on( a => sub { \& $p\->on( b => sub { \& $p\->on( c => sub { \& print "Woo!\en"; \& })\->walk; \& })\->walk; \& })\->walk; .Ve .PP Note that this shorthand only applies to \f(CW\*(C`on\*(C'\fR \- not to other methods that accept element names. .ie n .SS """inherit( [ element names ] )""" .el .SS "\f(CWinherit( [ element names ] )\fP" .IX Subsection "inherit( [ element names ] )" Inherit handlers from the containing scope. Typically used to import handlers that would otherwise be masked by a catch all '*' handler. .PP .Vb 8 \& $p\->on( \& \*(Aqa\*(Aq => sub { \& my ( $elem, $attr, $ctx ) = @_; \& my $link = $attr\->{href} || \*(Aq\*(Aq; \& my $text = $p\->text; \& print "Link: $text ($link)\en"; \& } \& ); \& \& $p\->on( \& \*(Aqspecial\*(Aq => sub { \& my ( $elem, $attr, $ctx ) = @_; \& \& # Within we want to handle all \& # tags apart from by printing them out \& $p\->on( \& \*(Aq*\*(Aq => sub { \& my ( $elem, $attr, $ctx ) = @_; \& print "Found: $elem\en"; \& } \& ); \& \& # Get the handler for from our containing \& # scope. \& $p\->inherit( \*(Aqa\*(Aq ); \& $p\->walk; \& } \& ); .Ve .PP The inherited handler is the handler that would have applied in the containing scope for an element with the given name. For example: .PP .Vb 11 \& $p\->on( \*(Aq*\*(Aq => sub { print "Whatever\en"; $p\->walk; } ); \& $p\->on( \& \*(Aqinteresting\*(Aq => sub { \& # Inherits the default \*(AqWhatever\*(Aq handler because that\*(Aqs the \& # handler that would have been called for in the \& # containing scope \& $p\->inherit( \*(Aqfrob\*(Aq ); \& # Handle everything else ourselves \& #p\->on(\*(Aq*\*(Aq, sub { $p\->walk; }); \& } \& ); .Ve .ie n .SS """before""" .el .SS "\f(CWbefore\fP" .IX Subsection "before" Register a handler to be called before the existing handler for an element. As with \f(CW\*(C`on\*(C'\fR multiple elements may be targeted by providing an array ref. .ie n .SS """after""" .el .SS "\f(CWafter\fP" .IX Subsection "after" Register a handler to be called after the existing handler for an element. As with \f(CW\*(C`on\*(C'\fR multiple elements may be targeted by providing an array ref. .ie n .SS """context""" .el .SS "\f(CWcontext\fP" .IX Subsection "context" Every time a handler is called a new scope is created for it. This allows nested handlers to be defined. The current scope contains a user context variable which can be used, for example, to keep track of an object that is being filled with values parsed from the \s-1XML.\s0 The context value is inherited from the parent scope but may be overridden locally. .PP For example: .PP .Vb 1 \& my $root = {}; \& \& # Set the outermost context \& $p\->context( $root ); \& \& # Handle HTML links /anywhere/ \& $p\->on( \& \*(Aqa\*(Aq => sub { \& my ( $elem, $attr, $ctx ) = @_; \& my $link = { \& href => $attr\->{href}, \& text => $p\->text \& }; \& push @{ $ctx\->{links} }, $link; \& } \& ); \& \& # Links in the body are stored in a nested \& # object. \& $p\->on( \& \*(Aqbody\*(Aq => sub { \& my ( $elem, $attr, $ctx ) = @_; \& my $body = {}; \& # Set the context \& $p\->context( $body ); \& $p\->walk; \& $ctx\->{body} = $body; \& } \& ); \& \& $p\->walk; .Ve .PP Note that the handler for tags stores its results in the current context object \- whatever that happens to be. That means that outside of any tag links will be stored in \f(CW$root\fR but within a they will be stored in a nested object (\f(CW\*(C`$root\->{body}\*(C'\fR). The handler itself need know nothing of this. .PP With no parameter \f(CW\*(C`context\*(C'\fR returns the current context. The current context is also passed as the third argument to handlers. .ie n .SS """text""" .el .SS "\f(CWtext\fP" .IX Subsection "text" Return any text contained within the current element. \s-1XML\s0 markup is discarded. .ie n .SS """xml""" .el .SS "\f(CWxml\fP" .IX Subsection "xml" Return the unparsed inner \s-1XML\s0 of the current element. For example: .PP .Vb 7 \& $p\->on( \& \*(Aqitem\*(Aq => sub { \& my ( $elem, $attr, $ctx ) = @_; \& my $item_source = $p\->xml; \& print "Item: $item_source\en"; \& } \& ); .Ve .PP If contains \s-1XHTML\s0 (for example) the above handler would correctly capture it without recursively parsing any elements it contains. Parsing .PP .Vb 4 \& \& This is the first story. \& This is another story. \& .Ve .PP would print .PP .Vb 2 \& Item: This is the first story. \& Item: This is another story. .Ve .ie n .SS """get_path""" .el .SS "\f(CWget_path\fP" .IX Subsection "get_path" Called within a handler returns the path that leads to the current element. For example: .PP .Vb 7 \& $p\->on( \& \*(Aqhere\*(Aq => sub { \& my ( $elem, $attr, $ctx ) = @_; \& print "I am here: ", $p\->get_path, "\en"; \& $p\->walk; \& } \& ); .Ve .PP would, if applied to this \s-1XML\s0 .PP .Vb 6 \& \& \& \& \& \& .Ve .PP print .PP .Vb 2 \& I am here: /outer/inner/here \& I am here: /outer/here .Ve .ie n .SS """get_token""" .el .SS "\f(CWget_token\fP" .IX Subsection "get_token" XML::Descent is built on \f(CW\*(C`XML::TokeParser\*(C'\fR which splits an \s-1XML\s0 document into a stream of tokens representing start tags, end tags, literal text, comment and processing instructions. Within an element \f(CW\*(C`get_token\*(C'\fR returns the same stream of tokens that \f(CW\*(C`XML::TokeParser\*(C'\fR would produce. Returns \f(CW\*(C`undef\*(C'\fR once all the tokens contained within the current element have been read (i.e. it's impossible to read past the end of the enclosed \s-1XML\s0). .ie n .SS """scope_handlers""" .el .SS "\f(CWscope_handlers\fP" .IX Subsection "scope_handlers" Get a list of all handlers that are registered locally to the current scope. The returned list won't include '*' if a wildcard handler has been registered. .ie n .SS """all_handlers""" .el .SS "\f(CWall_handlers\fP" .IX Subsection "all_handlers" Get a list of all registered handlers in all scopes. The returned list won't include the '*' wildcard handler. .SH "SEE ALSO" .IX Header "SEE ALSO" , XML::TokeParser, XML::Twig. .SH "BUGS AND LIMITATIONS" .IX Header "BUGS AND LIMITATIONS" XML::Descent uses \f(CW\*(C`XML::TokeParser\*(C'\fR to do the actual parsing. XML::TokeParser can only return start tags, end tags, raw text and processing instructions. As a result \f(CW\*(C`xml\*(C'\fR called at the root of an \s-1XML\s0 document will exclude any declaration. .PP No bugs have been reported. .PP Please report any bugs or feature requests to \&\f(CW\*(C`bug\-xml\-descent@rt.cpan.org\*(C'\fR, or through the web interface at . .SH "AUTHOR" .IX Header "AUTHOR" Andy Armstrong \f(CW\*(C`\*(C'\fR .SH "LICENCE AND COPYRIGHT" .IX Header "LICENCE AND COPYRIGHT" Copyright (c) 2006\-2009, Andy Armstrong \f(CW\*(C`\*(C'\fR. All rights reserved. .PP This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic. .SH "DISCLAIMER OF WARRANTY" .IX Header "DISCLAIMER OF WARRANTY" \&\s-1BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE \*(L"AS IS\*(R" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.\s0 .PP \&\s-1IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE\s0 (\s-1INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE\s0), \s-1EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\s0