XML::LibXML::Parser (3)
Leading comments
Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) Standard preamble: ========================================================================
NAME
XML::LibXML::Parser - Parsing XML Data with XML::LibXMLSYNOPSIS
use XML::LibXML '1.70'; # Parser constructor $parser = XML::LibXML->new(); $parser = XML::LibXML->new(option=>value, ...); $parser = XML::LibXML->new({option=>value, ...}); # Parsing XML $dom = XML::LibXML->load_xml( location => $file_or_url # parser options ... ); $dom = XML::LibXML->load_xml( string => $xml_string # parser options ... ); $dom = XML::LibXML->load_xml( string => (\$xml_string) # parser options ... ); $dom = XML::LibXML->load_xml({ IO => $perl_file_handle # parser options ... ); $dom = $parser->load_xml(...); # Parsing HTML $dom = XML::LibXML->load_html(...); $dom = $parser->load_html(...); # Parsing well-balanced XML chunks $fragment = $parser->parse_balanced_chunk( $wbxmlstring, $encoding ); # Processing XInclude $parser->process_xincludes( $doc ); $parser->processXIncludes( $doc ); # Old-style parser interfaces $doc = $parser->parse_file( $xmlfilename ); $doc = $parser->parse_fh( $io_fh ); $doc = $parser->parse_string( $xmlstring); $doc = $parser->parse_html_file( $htmlfile, \%opts ); $doc = $parser->parse_html_fh( $io_fh, \%opts ); $doc = $parser->parse_html_string( $htmlstring, \%opts ); # Push parser $parser->parse_chunk($string, $terminate); $parser->init_push(); $parser->push(@data); $doc = $parser->finish_push( $recover ); # Set/query parser options $parser->option_exists($name); $parser->get_option($name); $parser->set_option($name,$value); $parser->set_options({$name=>$value,...}); # XML catalogs $parser->load_catalog( $catalog_file );
PARSING
An- *
-
A DOMPull-Parser
- *
-
A DOMPush-Parser
- *
-
A SAXParser
- *
-
A DOMbasedSAXParser.
Creating a Parser Instance
XML::LibXML provides an- new
-
$parser = XML::LibXML->new(); $parser = XML::LibXML->new(option=>value, ...); $parser = XML::LibXML->new({option=>value, ...});
Create a new
XMLandHTMLparser instance. Each parser instance holds default values for various parser options. Optionally, one can pass a hash reference or a list of option => value pairs to set a different default set of options. Unless specified otherwise, the options "load_ext_dtd", and "expand_entities" are set to 1. See ``Parser Options'' for a list of libxml2 parser's options.
DOM Parser
One of the common parser interfaces of XML::LibXML is the XML::LibXML's
All of the functions listed below will throw an exception if the document is invalid. To prevent this causing your program exiting, wrap the call in an eval{} block
- load_xml
-
$dom = XML::LibXML->load_xml( location => $file_or_url # parser options ... ); $dom = XML::LibXML->load_xml( string => $xml_string # parser options ... ); $dom = XML::LibXML->load_xml( string => (\$xml_string) # parser options ... ); $dom = XML::LibXML->load_xml({ IO => $perl_file_handle # parser options ... ); $dom = $parser->load_xml(...);
This function is available since XML::LibXML 1.70. It provides easy to use interface to the
XMLparser that parses given file (orURL), string, or input stream to aDOMtree. The arguments can be passed in aHASHreference or as name => value pairs. The function can be called as a class method or an object method. In both cases it internally creates a new parser instance passing the specified parser options; if called as an object method, it clones the original parser (preserving its settings) and additionally applies the specified options to the new parser. See the constructor "new" and ``Parser Options'' for more information. - load_html
-
$dom = XML::LibXML->load_html(...); $dom = $parser->load_html(...);
This function is available since XML::LibXML 1.70. It has the same usage as "load_xml", providing interface to the
HTMLparser. See "load_xml" for more information.
Parsing
The functions described above are implemented to parse well formed documents. In some cases a program gets well balanced
- parse_balanced_chunk
-
$fragment = $parser->parse_balanced_chunk( $wbxmlstring, $encoding );
This function parses a well balanced
XMLstring into a XML::LibXML::DocumentFragment. The first arguments contains the input string, the optional second argument can be used to specify character encoding of the input (UTF-8is assumed by default). - parse_xml_chunk
- This is the old name of parse_balanced_chunk(). Because it may causes confusion with the push parser interface, this function should not be used anymore.
By default XML::LibXML does not process XInclude tags within an
- process_xincludes
-
$parser->process_xincludes( $doc );
After a document is parsed into a
DOMstructure, you may want to expand the documents XInclude tags. This function processes the given document structure and expands all XInclude tags (or throws an error) by using the flags and callbacks of the given parser instance.Note that the resulting Tree contains some extra nodes (of type
XML_XINCLUDE_STARTandXML_XINCLUDE_END) after successfully processing the document. These nodes indicate where data was included into the original tree. if the document is serialized, these extra nodes will not show up.Remember: A Document with processed XIncludes differs from the original document after serialization, because the original XInclude tags will not get restored!
If the parser flag ``expand_xincludes'' is set to 1, you need not to post process the parsed document.
- processXIncludes
-
$parser->processXIncludes( $doc );
This is an alias to process_xincludes, but through a
JAVAlike function name. - parse_file
-
$doc = $parser->parse_file( $xmlfilename );
This function parses an
XMLdocument from a file or network; $xmlfilename can be either a filename or anURL.Note that for parsing files, this function is the fastest choice, about 6-8 times faster then parse_fh(). - parse_fh
-
$doc = $parser->parse_fh( $io_fh );
parse_fh() parses a
IOREFor a subclass of IO::Handle.Because the data comes from an open handle, libxml2's parser does not know about the base
URIof the document. To set the baseURIone should use parse_fh() as follows:my $doc = $parser->parse_fh( $io_fh, $baseuri );
- parse_string
-
$doc = $parser->parse_string( $xmlstring);
This function is similar to parse_fh(), but it parses an
XMLdocument that is available as a single string in memory, or alternatively as a reference to a scalar containing a string. Again, you can pass an optional baseURIto the function.my $doc = $parser->parse_string( $xmlstring, $baseuri ); my $doc = $parser->parse_string(\$xmlstring, $baseuri);
- parse_html_file
-
$doc = $parser->parse_html_file( $htmlfile, \%opts );
Similar to parse_file() but parses
HTML(strict) documents; $htmlfile can be filename orURL.An optional second argument can be used to pass some options to the
HTMLparser as aHASHreference. See options labeled withHTMLin ``Parser Options''. - parse_html_fh
-
$doc = $parser->parse_html_fh( $io_fh, \%opts );
Similar to parse_fh() but parses
HTML(strict) streams.An optional second argument can be used to pass some options to the
HTMLparser as aHASHreference. See options labeled withHTMLin ``Parser Options''.Note: encoding option may not work correctly with this function in libxml2 < 2.6.27 if the
HTMLfile declares charset using aMETAtag. - parse_html_string
-
$doc = $parser->parse_html_string( $htmlstring, \%opts );
Similar to parse_string() but parses
HTML(strict) strings.An optional second argument can be used to pass some options to the
HTMLparser as aHASHreference. See options labeled withHTMLin ``Parser Options''.
Push Parser
XML::LibXML provides a push parser interface. Rather than pulling the data from a given source the push parser waits for the data to be pushed into it.This allows one to parse large documents without waiting for the parser to finish. The interface is especially useful if a program needs to pre-process the incoming pieces of
While XML::LibXML parse_*() functions force the data to be a well-formed
Different to the pull parser implemented in parse_fh() or parse_file(), the push parser is not able to find out about the documents end itself. Thus the calling program needs to indicate explicitly when the parsing is done.
In XML::LibXML this is done by a single function:
- parse_chunk
-
$parser->parse_chunk($string, $terminate);
parse_chunk() tries to parse a given chunk of data, which isn't necessarily well balanced data. The function takes two parameters: The chunk of data as a string and optional a termination flag. If the termination flag is set to a true value (e.g. 1), the parsing will be stopped and the resulting document will be returned as the following example describes:
my $parser = XML::LibXML->new; for my $string ( "<", "foo", ' bar="hello world"', "/>") { $parser->parse_chunk( $string ); } my $doc = $parser->parse_chunk("", 1); # terminate the parsing
Internally XML::LibXML provides three functions that control the push parser process:
- init_push
-
$parser->init_push();
Initializes the push parser.
- push
-
$parser->push(@data);
This function pushes the data stored inside the array to libxml2's parser. Each entry in @data must be a normal scalar! This method can be called repeatedly.
- finish_push
-
$doc = $parser->finish_push( $recover );
This function returns the result of the parsing process. If this function is called without a parameter it will complain about non well-formed documents. If $restore is 1, the push parser can be used to restore broken or non well formed (
XML) documents as the following example shows:eval { $parser->push( "<foo>", "bar" ); $doc = $parser->finish_push(); # will report broken XML }; if ( $@ ) { # ... }
This can be annoying if the closing tag is missed by accident. The following code will restore the document:
eval { $parser->push( "<foo>", "bar" ); $doc = $parser->finish_push(1); # will return the data parsed # unless an error happened }; print $doc->toString(); # returns "<foo>bar</foo>"
Of course finish_push() will return nothing if there was no data pushed to the parser before.
Pull Parser (Reader)
XML::LibXML also provides a pull-parser interface similar to the XmlReader interface in .NET. This interface is almost streaming, and is usually faster and simpler to use thanDirect SAX Parser
XML::LibXML provides a direct DOM based SAX Parser
XML::LibXML also provides a The
Aside from the regular parsing methods, you can access the
my $doc = build_yourself_a_document(); my $saxparser = $XML::LibXML::SAX::Parser->new( ... ); $parser->generate( $doc );
This is useful for serializing
This is
SERIALIZATION
XML::LibXML provides some functions to serialize nodes and documents. The serialization functions are described on the XML::LibXML::Node manpage or the XML::LibXML::Document manpage. XML::LibXML checks three global flags that alter the serialization process:- *
- skipXMLDeclaration
- *
- skipDTD
- *
- setTagCompression
of that three functions only setTagCompression is available for all serialization functions.
Because XML::LibXML does these flags not itself, one has to define them locally as the following example shows:
local $XML::LibXML::skipXMLDeclaration = 1; local $XML::LibXML::skipDTD = 1; local $XML::LibXML::setTagCompression = 1;
If skipXMLDeclaration is defined and not '0', the
If skipDTD is defined and not '0', an existing
If setTagCompression is defined and not '0' empty tags are displayed as open and closing tags rather than the shortcut. For example the empty tag foo will be rendered as <foo></foo> rather than <foo/>.
PARSER OPTIONS
Handling of libxml2 parser options has been unified and improved in XML::LibXML 1.70. You can now set default options for a particular parser instance by passing them to the constructor as "XML::LibXML->new({name=>value, ...})" or "XML::LibXML->new(name=>value,...)". The options can be queried and changed using the following methods (pre-1.70 interfaces such as "$parser->load_ext_dtd(0)" also exist, see below):- option_exists
-
$parser->option_exists($name);
Returns 1 if the current XML::LibXML version supports the option $name, otherwise returns 0 (note that this does not necessarily mean that the option is supported by the underlying libxml2 library).
- get_option
-
$parser->get_option($name);
Returns the current value of the parser option $name.
- set_option
-
$parser->set_option($name,$value);
Sets option $name to value $value.
- set_options
-
$parser->set_options({$name=>$value,...});
Sets multiple parsing options at once.
Each of the flags listed below is labeled
- /parser/
- if it can be used with a "XML::LibXML" parser object (i.e. passed to "XML::LibXML->new", "XML::LibXML->set_option", etc.)
- /html/
- if it can be used passed to the "parse_html_*" methods
- /reader/
- if it can be used with the "XML::LibXML::Reader".
Unless specified otherwise, the default for boolean valued options is 0 (false).
The available options are:
- URI
-
/parser, html, reader/
In case of parsing strings or file handles, XML::LibXML doesn't know about the base uri of the document. To make relative references such as XIncludes work, one has to set a base
URI,that is then used for the parsed document. - line_numbers
-
/parser, html, reader/
If this option is activated, libxml2 will store the line number of each element node in the parsed document. The line number can be obtained using the "line_number()" method of the "XML::LibXML::Node" class (for non-element nodes this may report the line number of the containing element). The line numbers are also used for reporting positions of validation errors.
IMPORTANT:Due to limitations in the libxml2 library line numbers greater than 65535 will be returned as 65535. Unfortunately, this is a long and sad story, please see <bugzilla.gnome.org/show_bug.cgi?id=325533> for more details. - encoding
-
/html/
character encoding of the input
- recover
-
/parser, html, reader/
recover from errors; possible values are 0, 1, and 2
A true value turns on recovery mode which allows one to parse broken
XMLorHTMLdata. The recovery mode allows the parser to return the successfully parsed portion of the input document. This is useful for almost well-formed documents, where for example a closing tag is missing somewhere. Still, XML::LibXML will only parse until the first fatal (non-recoverable) error occurs, reporting recoverable parsing errors as warnings. To suppress even these warnings, use recover=>2.Note that validation is switched off automatically in recovery mode.
- expand_entities
-
/parser, reader/
substitute entities; possible values are 0 and 1; default is 1
Note that although this flag disables entity substitution, it does not prevent the parser from loading external entities; when substitution of an external entity is disabled, the entity will be represented in the document tree by an
XML_ENTITY_REF_NODEnode whose subtree will be the content obtained by parsing the external resource; Although this nesting is visible from theDOMit is transparent to XPath data model, so it is possible to match nodes in an unexpanded entity by the same XPath expression as if the entity were expanded. See also ext_ent_handler. - ext_ent_handler
-
/parser/
Provide a custom external entity handler to be used when expand_entities is set to 1. Possible value is a subroutine reference.
This feature does not work properly in libxml2 < 2.6.27!
The subroutine provided is called whenever the parser needs to retrieve the content of an external entity. It is called with two arguments: the system
ID(URI) and the publicID.The value returned by the subroutine is parsed as the content of the entity.This method can be used to completely disable entity loading, e.g. to prevent exploits of the type described at (<searchsecuritychannel.techtarget.com/generic/0,295582,sid97_gci1304703,00.html>), where a service is tricked to expose its private data by letting it parse a remote file (
RSSfeed) that contains an entity reference to a local file (e.g. "/etc/fstab").A more granular solution to this problem, however, is provided by custom
URLresolvers, as inmy $c = XML::LibXML::InputCallback->new(); sub match { # accept file:/ URIs except for XML catalogs in /etc/xml/ my ($uri) = @_; return ($uri=~m{^file:/} and $uri !~ m{^file:///etc/xml/}) ? 1 : 0; } $c->register_callbacks([ \&match, sub{}, sub{}, sub{} ]); $parser->input_callbacks($c);
- load_ext_dtd
-
/parser, reader/
load the external
DTDsubset while parsing; possible values are 0 and 1. Unless specified, XML::LibXML sets this option to 1.This flag is also required for
DTDValidation, to provide complete attribute, and to expand entities, regardless if the document has an internal subset. Thus switching off externalDTDloading, will disable entity expansion, validation, and complete attributes on internal subsets as well. - complete_attributes
-
/parser, reader/
create default
DTDattributes; possible values are 0 and 1 - validation
-
/parser, reader/
validate with the
DTD; possible values are 0 and 1 - suppress_errors
-
/parser, html, reader/
suppress error reports; possible values are 0 and 1
- suppress_warnings
-
/parser, html, reader/
suppress warning reports; possible values are 0 and 1
- pedantic_parser
-
/parser, html, reader/
pedantic error reporting; possible values are 0 and 1
- no_blanks
-
/parser, html, reader/
remove blank nodes; possible values are 0 and 1
- no_defdtd
-
/html/
do not add a default
DOCTYPE; possible values are 0 and 1the default is (0) to add a
DTDwhen the input html lacks one - expand_xinclude or xinclude
-
/parser, reader/
Implement XInclude substitution; possible values are 0 and 1
Expands XInclude tags immediately while parsing the document. Note that the parser will use the
URIresolvers installed via "XML::LibXML::InputCallback" to parse the included document (if any). - no_xinclude_nodes
-
/parser, reader/
do not generate
XINCLUDE START/ENDnodes; possible values are 0 and 1 - no_network
-
/parser, html, reader/
Forbid network access; possible values are 0 and 1
If set to true, all attempts to fetch non-local resources (such as
DTDor external entities) will fail (unless custom callbacks are defined).It may be necessary to use the flag "recover" for processing documents requiring such resources while networking is off.
- clean_namespaces
-
/parser, reader/
remove redundant namespaces declarations during parsing; possible values are 0 and 1.
- no_cdata
-
/parser, html, reader/
merge
CDATAas text nodes; possible values are 0 and 1 - no_basefix
-
/parser, reader/
not fixup
XINCLUDExml#baseURIS; possible values are 0 and 1 - huge
-
/parser, html, reader/
relax any hardcoded limit from the parser; possible values are 0 and 1. Unless specified, XML::LibXML sets this option to 0.
Note: the default value for this option was changed to protect against denial of service through entity expansion attacks. Before enabling the option ensure you have taken alternative measures to protect your application against this type of attack.
- gdome
-
/parser/
THIS OPTION IS EXPERIMENTAL!
Although quite powerful, XML::LibXML's
DOMimplementation is incomplete with respect to theDOMlevel 2 or level 3 specifications.XML::GDOMEis based on libxml2 as well, and provides a rather completeDOMimplementation by wrapping libgdome. This flag allows you to make use of XML::LibXML's full parser options andXML::GDOME'sDOMimplementation at the same time.To make use of this function, one has to install libgdome and configure XML::LibXML to use this library. For this you need to rebuild XML::LibXML!
Note: this feature was not seriously tested in recent XML::LibXML releases.
For compatibility with XML::LibXML versions prior to 1.70, the following methods are also supported for querying and setting the corresponding parser options (if called without arguments, the methods return the current value of the corresponding parser options; with an argument sets the option to a given value):
$parser->validation(); $parser->recover(); $parser->pedantic_parser(); $parser->line_numbers(); $parser->load_ext_dtd(); $parser->complete_attributes(); $parser->expand_xinclude(); $parser->gdome_dom(); $parser->clean_namespaces(); $parser->no_network();
The following obsolete methods trigger parser options in some special way:
- recover_silently
-
$parser->recover_silently(1);
If called without an argument, returns true if the current value of the "recover" parser option is 2 and returns false otherwise. With a true argument sets the "recover" parser option to 2; with a false argument sets the "recover" parser option to 0.
- expand_entities
-
$parser->expand_entities(0);
Get/set the "expand_entities" option. If called with a true argument, also turns the "load_ext_dtd" option to 1.
- keep_blanks
-
$parser->keep_blanks(0);
This is actually the opposite of the "no_blanks" parser option. If used without an argument retrieves negated value of "no_blanks". If used with an argument sets "no_blanks" to the opposite value.
- base_uri
-
$parser->base_uri( $your_base_uri );
Get/set the "URI" option.
XML CATALOGS
"libxml2" supportsNote that libxml2 has a global pool of loaded catalogs, so if you apply the method "load_catalog" to one parser instance, all parser instances will start using the catalog (in addition to other previously loaded catalogs).
Note also that catalogs are not used when a custom external entity handler is specified. At the current state it is not possible to make use of both types of resolving systems at the same time.
- load_catalog
-
$parser->load_catalog( $catalog_file );
Loads the
XMLcatalog file $catalog_file.# Global external entity loader (similar to ext_ent_handler option # but this works really globally, also in XML::LibXSLT include etc..) XML::LibXML::externalEntityLoader(\&my_loader);
ERROR REPORTING
XML::LibXML throws exceptions during parsing, validation or XPath processing (and some other occasions). These errors can be caught by using eval blocks. The error is stored in $@. There are two implementations: the old one throws $@ which is just a message string, in the new one $@ is an object from the class XML::LibXML::Error; this class overrides the operator "" so that when printed, the object flattens to the usual error message.XML::LibXML throws errors as they occur. This is a very common misunderstanding in the use of XML::LibXML. If the eval is omitted, XML::LibXML will always halt your script by ``croaking'' (see Carp man page for details).
Also note that an increasing number of functions throw errors if bad data is passed as arguments. If you cannot assure valid data passed to XML::LibXML you should eval these functions.
Note: since version 1.59, get_last_error() is no longer available in XML::LibXML for thread-safety reasons.
AUTHORS
Matt Sergeant, Christian Glahn, Petr PajasVERSION
2.0122COPYRIGHT
2001-2007, AxKit.com Ltd.2002-2006, Christian Glahn.
2006-2009, Petr Pajas.