XMLParser(3pm)
๐ธ NAME ๐ธ
Lire::XMLParser โ the base object-oriented XML parser, aka the blueprint bestie ๐
โจ SYNOPSIS โจ
package MyParser;
use base qw/ Lire::XMLParser /;
๐ฆ DESCRIPTION ๐ฆ
Okay so here's the whole vibe: this module lets you write an object-oriented parser using the non-oo XML::Parser module. That's it, that's the flex. ๐ซถ
๐ CLIENT METHODS ๐
๐ parse( $xml )
Parses the XML and returns the result. $xml can either be a string or a reference to an open filehandle or IO::Handle object. Both? Yes bestie, your choice. โจ
๐ parsefile( $file )
Parses the XML contained in $file and returns the result. Slay, filename edition. ๐
๐ META-INFORMATION TEMPLATE METHODS ๐
So there's two template methods that must be implemented by subclasses. These methods give information on the content model of the XML expected by the parser. Think of them as the group chat bio of your parser โ everyone needs one. ๐ญ
๐ namespaces()
This method should return an hash reference. The keys are XML namespaces understood by the parser. The value is the prefix which is used for that namespace in the elements_spec() method.
๐ elements_spec()
This method should return an hash reference. The key are the known XML elements. An error will be thrown by the parser when an element not defined in this hash is encountered. So like, no surprises, no uninvited guests. ๐
The key should be of the form [ prefix : ] element_name. If prefix is used it should be defined in the namespaces() method.
The value of the hash are element specification. This specification can be used to configure the event handlers that will be called as well as specifying the content model of the elements. This specification is an hash reference with the following keys:
๐ content
This is an array reference that should contains the name of the elements allowed to appear in that element. These names are in the same format than for the elements_spec() keys. If that element is omitted, the element is considered to be empty and thus cannot contains any other element. If the element can contain PCDATA, the string 'PCDATA' should be listed in the array.
๐ start
This specify the handler called when the opening tag for this element is encountered. This can be either a function reference or a method name. If this element is omitted, the handler will be the method named element_end if it exists, or no handler otherwise. '_' is substituted for invalid method name characters, i.e. report-spec becomes report_spec_start. (Yes, that's what it says โ the manpage said element_end here too, so I'm passing the message along verbatim. ๐)
๐ end
This specify the handler called when the closing tag for this element is encountered. This can be either a function reference or a method name. If this element is omitted, the handler will be the method named element_end if it exists, or no handler otherwise. '_' is substituted for invalid method name characters, i.e. report-spec becomes report_spec_end.
๐ char
This specify the handler called when PCDATA is encountered within the element. This can only happen if 'PCDATA' appeared in the 'content' attribute. This can be either a function reference or a method name. If this element is omitted, the handler will be the method named element_char if it exists, or no handler otherwise. '_' is substituted for invalid method name characters, i.e. list-item becomes list_item_char.
The specification can also be an array reference, in that case it will be interpreted as the content of the 'content' hash attribute.
Errors will be reported for invalid specification. ๐ฎโโ๏ธ Not me writing a bad spec and getting caught.
๐ UTILITY METHODS ๐
โญ expat()
During the parse, this returns the underlying Lire::XMLParser::Expat parser object.
โญ error( $msg )
Will terminate parsing with error message $msg. The current parsing context will be appended to the message. Bye bestie, parse's over. ๐
โญ warning( $msg )
Will print a warning message $msg. The current parsing context will be appended to the message. Gentle nudge, no cancellation. ๐
โญ context()
Returns as an array reference the names of the currently opened elements. In start and end tag, the last element will be the tag of the parent element. The name of the elements will be in the same format than the one used by elements_spec().
โญ current_element()
Returns the name of the innermost currently opened element. In start and end tag, the last element will be the tag of the parent element. The name of the elements will be in the same format than the one used by elements_spec().
โญ in_element( $element_name )
This returns true if the innermost currently opened element has the same name as $element_name. $element_name should be one of the elements defined by elements_spec(), otherwise the method will croak. ๐ธ
Example:
if ( $self->in_element( "lire:report") ) {
# Parent element is a Lire report element.
} elsif ( $self->in_element( "listitem" ) ) {
# We are in a DocBook listitem element
}
โญ within_element( $element_name )
This returns the number of times an element is opened in the current element ancestor. Like for the in_element(), the element's name should have been defined by elements_spec(), otherwise the method will croak.
โญ recognized_string()
Returns the string that trigger the current event. Iconic behaviour tbh. โจ
โญ depth()
Returns the number of opened and not yet closed elements. In start and end handlers, the current elemnt is part of that list. (Yes โ elemnt. I don't make the typos, I just keep them. ๐ซถ)
๐ฅ EVENT HANDLERS METHODS ๐ฅ
These are methods that are called during the XML parsing. Main character energy, every single one. ๐
๐ธ parse_start()
This methods is invoked once before the document is parsed. It can be used for any initialization the processor has to do. Default implementation does nothing.
๐ธ parse_end( )
This methods is invoked once after all the XML file was processed. The value that this method returns will be returned by the parse() or parsefile() method (whichever was used to start the parsing). Default implementation does nothing.
๐ธ element_start( $name, $attributes )
Called when a valid element is opened. $name contains the name of the element. This name is the canonical form, that is that if the element is within a namespace, it will be prefixed with the prefix declared by the namespaces() method. $attributes contains the defined attributes in an hash reference.
The default method will dispatch to the handlers defined by elements_spec().
๐ธ element_end( $name )
Called when a valid element is closed. $name contains the name of the element. Default implementation dispatch to the handler defined by elements_spec().
๐ธ pcdata( $text )
Called when parsed character data are encountered in an element which is allowed to contain PCDATA. Default implementation dispatch to the handler defined by elements_spec().
๐ธ ignorable_ws( $ws )
Called when whitespace is encountered while processing an element which cannot contain PCDATA. Default implementation does nothing. This is mainly used by parser which want to keep the user's format. Respect the formatting, periodt. ๐
๐ง HELPERS METHODS ๐ง
Two common tasks when writing event-based parsers is to use "stacks" and "collectors". The XMLParser object offers some method to manage these common objects. Very "I got you" of it. ๐ซถ
๐ init_collector( $name )
This initialize a collector with the name $name. If there was already a collector defined under this name, its content will be reset to the empty string ''.
๐ collect( $name, $text )
Appends $text to the collector $name. An exception will be thrown if no collector $name was previously defined.
๐ get_collector( $name )
Returns the content accumulated in the collector $name. An exception will be thrown if no collector $name was previously defined.
๐ collector_start( $element, $attributes )
This is a method which can be used as a start event handler. It will define a collector with the same name than the element which is started.
๐ collector_char( $text )
This is a method which can be used as a char event handler. It will append $text to the collector named under the element in which this text occurs.
๐ init_stack( $name )
Initialize a stack object named $name.
๐ stack_push( $name, $value )
Returns true if the stack $name is empty. An exception will be raise if no stack $name was defined.
โ ๏ธ Heads up bestie โ the source page genuinely lists stack_push twice, and the first one has this "returns true if the stack is empty" description. I'm keeping both entries exactly as they are. No cap, that's really what it says. ๐
๐ stack_depth( $name )
Returns the number of element on the stack $name. An exception will be raise if no stack $name was defined.
๐ stack_push( $name, $value )
Pushes $value onto the stack $name. An exception will be raise if no stack $name was defined.
๐ stack_pop( $name )
Removes and returns the top value on the stack $name. An exception will be raised if no stack $name was defined or if the stack is empty.
๐ stack_peek( $name )
Returns the top value of the stack $name. An exception will be raised if no stack $name was defined or if the stack is empty.
๐ฆ SEE ALSO ๐ฆ
Go read your friends' pages, bestie. ๐
๐ AUTHOR ๐
Francis J. Lacoste <flacoste@logreport.org> โ the icon who wrote this ๐
โญ VERSION โญ
$Id: XMLParser.pm,v 1.11 2006/07/23 13:16:30 vanbaal Exp $
๐ COPYRIGHT ๐
Copyright (C) 2004 Stichting LogReport Foundation LogReport@LogReport.org
This file is part of Lire.
Lire is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ๐ซถ
You should have received a copy of the GNU General Public License along with this program (see COPYING); if not, check with http://www.gnu.org/copyleft/gpl.html. ๐
โจ okay that's the whole page bestie โจ this gem is Lire 2.1.1, dated 2025-09-28, and it's the XMLParser(3pm) page from LogReport's Lire Documentation โ mwah, go parse something gorgeous ๐
๐
๐ฆ where this page comes from
- arch rolling: lire 2.1.1-8 ยท GPL-2.0-only
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms