| 1 |
=head1 NAME |
| 2 |
|
| 3 |
XML::LibXML::Parser - Parsing XML Data with XML::LibXML |
| 4 |
|
| 5 |
=head1 SYNOPSIS |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
use XML::LibXML '1.70'; |
| 10 |
|
| 11 |
# Parser constructor |
| 12 |
|
| 13 |
$parser = XML::LibXML->new(); |
| 14 |
$parser = XML::LibXML->new(option=>value, ...); |
| 15 |
$parser = XML::LibXML->new({option=>value, ...}); |
| 16 |
|
| 17 |
# Parsing XML |
| 18 |
|
| 19 |
$dom = XML::LibXML->load_xml( |
| 20 |
location => $file_or_url |
| 21 |
# parser options ... |
| 22 |
); |
| 23 |
$dom = XML::LibXML->load_xml( |
| 24 |
string => $xml_string |
| 25 |
# parser options ... |
| 26 |
); |
| 27 |
$dom = XML::LibXML->load_xml( |
| 28 |
string => (\$xml_string) |
| 29 |
# parser options ... |
| 30 |
); |
| 31 |
$dom = XML::LibXML->load_xml({ |
| 32 |
IO => $perl_file_handle |
| 33 |
# parser options ... |
| 34 |
); |
| 35 |
$dom = $parser->load_xml(...); |
| 36 |
|
| 37 |
# Parsing HTML |
| 38 |
|
| 39 |
$dom = XML::LibXML->load_html(...); |
| 40 |
$dom = $parser->load_html(...); |
| 41 |
|
| 42 |
# Parsing well-balanced XML chunks |
| 43 |
|
| 44 |
$fragment = $parser->parse_balanced_chunk( $wbxmlstring, $encoding ); |
| 45 |
|
| 46 |
# Processing XInclude |
| 47 |
|
| 48 |
$parser->process_xincludes( $doc ); |
| 49 |
$parser->processXIncludes( $doc ); |
| 50 |
|
| 51 |
# Old-style parser interfaces |
| 52 |
|
| 53 |
$doc = $parser->parse_file( $xmlfilename ); |
| 54 |
$doc = $parser->parse_fh( $io_fh ); |
| 55 |
$doc = $parser->parse_string( $xmlstring); |
| 56 |
$doc = $parser->parse_html_file( $htmlfile, \%opts ); |
| 57 |
$doc = $parser->parse_html_fh( $io_fh, \%opts ); |
| 58 |
$doc = $parser->parse_html_string( $htmlstring, \%opts ); |
| 59 |
|
| 60 |
# Push parser |
| 61 |
|
| 62 |
$parser->parse_chunk($string, $terminate); |
| 63 |
$parser->init_push(); |
| 64 |
$parser->push(@data); |
| 65 |
$doc = $parser->finish_push( $recover ); |
| 66 |
|
| 67 |
# Set/query parser options |
| 68 |
|
| 69 |
$parser->option_exists($name); |
| 70 |
$parser->get_option($name); |
| 71 |
$parser->set_option($name,$value); |
| 72 |
$parser->set_options({$name=>$value,...}); |
| 73 |
|
| 74 |
# XML catalogs |
| 75 |
|
| 76 |
$parser->load_catalog( $catalog_file ); |
| 77 |
|
| 78 |
=head1 PARSING |
| 79 |
|
| 80 |
An XML document is read into a data structure such as a DOM tree by a piece of |
| 81 |
software, called a parser. XML::LibXML currently provides four different parser |
| 82 |
interfaces: |
| 83 |
|
| 84 |
|
| 85 |
=over 4 |
| 86 |
|
| 87 |
=item * |
| 88 |
|
| 89 |
A DOM Pull-Parser |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
=item * |
| 94 |
|
| 95 |
A DOM Push-Parser |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
=item * |
| 100 |
|
| 101 |
A SAX Parser |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
=item * |
| 106 |
|
| 107 |
A DOM based SAX Parser. |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
=back |
| 112 |
|
| 113 |
|
| 114 |
=head2 Creating a Parser Instance |
| 115 |
|
| 116 |
XML::LibXML provides an OO interface to the libxml2 parser functions. Thus you |
| 117 |
have to create a parser instance before you can parse any XML data. |
| 118 |
|
| 119 |
=over 4 |
| 120 |
|
| 121 |
=item new |
| 122 |
|
| 123 |
|
| 124 |
$parser = XML::LibXML->new(); |
| 125 |
$parser = XML::LibXML->new(option=>value, ...); |
| 126 |
$parser = XML::LibXML->new({option=>value, ...}); |
| 127 |
|
| 128 |
Create a new XML and HTML parser instance. Each parser instance holds default |
| 129 |
values for various parser options. Optionally, one can pass a hash reference or |
| 130 |
a list of option => value pairs to set a different default set of options. |
| 131 |
Unless specified otherwise, the options C<<<<<< load_ext_dtd >>>>>>, and C<<<<<< expand_entities >>>>>> are set to 1. See L<<<<<< Parser Options >>>>>> for a list of libxml2 parser's options. |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
=back |
| 136 |
|
| 137 |
|
| 138 |
=head2 DOM Parser |
| 139 |
|
| 140 |
One of the common parser interfaces of XML::LibXML is the DOM parser. This |
| 141 |
parser reads XML data into a DOM like data structure, so each tag can get |
| 142 |
accessed and transformed. |
| 143 |
|
| 144 |
XML::LibXML's DOM parser is not only capable to parse XML data, but also |
| 145 |
(strict) HTML files. There are three ways to parse documents - as a string, as |
| 146 |
a Perl filehandle, or as a filename/URL. The return value from each is a L<<<<<< XML::LibXML::Document >>>>>> object, which is a DOM object. |
| 147 |
|
| 148 |
All of the functions listed below will throw an exception if the document is |
| 149 |
invalid. To prevent this causing your program exiting, wrap the call in an |
| 150 |
eval{} block |
| 151 |
|
| 152 |
=over 4 |
| 153 |
|
| 154 |
=item load_xml |
| 155 |
|
| 156 |
|
| 157 |
$dom = XML::LibXML->load_xml( |
| 158 |
location => $file_or_url |
| 159 |
# parser options ... |
| 160 |
); |
| 161 |
$dom = XML::LibXML->load_xml( |
| 162 |
string => $xml_string |
| 163 |
# parser options ... |
| 164 |
); |
| 165 |
$dom = XML::LibXML->load_xml( |
| 166 |
string => (\$xml_string) |
| 167 |
# parser options ... |
| 168 |
); |
| 169 |
$dom = XML::LibXML->load_xml({ |
| 170 |
IO => $perl_file_handle |
| 171 |
# parser options ... |
| 172 |
); |
| 173 |
$dom = $parser->load_xml(...); |
| 174 |
|
| 175 |
|
| 176 |
This function is available since XML::LibXML 1.70. It provides easy to use |
| 177 |
interface to the XML parser that parses given file (or URL), string, or input |
| 178 |
stream to a DOM tree. The arguments can be passed in a HASH reference or as |
| 179 |
name => value pairs. The function can be called as a class method or an object |
| 180 |
method. In both cases it internally creates a new parser instance passing the |
| 181 |
specified parser options; if called as an object method, it clones the original |
| 182 |
parser (preserving its settings) and additionally applies the specified options |
| 183 |
to the new parser. See the constructor C<<<<<< new >>>>>> and L<<<<<< Parser Options >>>>>> for more information. |
| 184 |
|
| 185 |
|
| 186 |
=item load_html |
| 187 |
|
| 188 |
|
| 189 |
$dom = XML::LibXML->load_html(...); |
| 190 |
$dom = $parser->load_html(...); |
| 191 |
|
| 192 |
|
| 193 |
This function is available since XML::LibXML 1.70. It has the same usage as C<<<<<< load_xml >>>>>>, providing interface to the HTML parser. See C<<<<<< load_xml >>>>>> for more information. |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
=back |
| 198 |
|
| 199 |
Parsing HTML may cause problems, especially if the ampersand ('&') is used. |
| 200 |
This is a common problem if HTML code is parsed that contains links to |
| 201 |
CGI-scripts. Such links cause the parser to throw errors. In such cases libxml2 |
| 202 |
still parses the entire document as there was no error, but the error causes |
| 203 |
XML::LibXML to stop the parsing process. However, the document is not lost. |
| 204 |
Such HTML documents should be parsed using the I<<<<<< recover >>>>>> flag. By default recovering is deactivated. |
| 205 |
|
| 206 |
The functions described above are implemented to parse well formed documents. |
| 207 |
In some cases a program gets well balanced XML instead of well formed documents |
| 208 |
(e.g. an XML fragment from a database). With XML::LibXML it is not required to |
| 209 |
wrap such fragments in the code, because XML::LibXML is capable even to parse |
| 210 |
well balanced XML fragments. |
| 211 |
|
| 212 |
=over 4 |
| 213 |
|
| 214 |
=item parse_balanced_chunk |
| 215 |
|
| 216 |
$fragment = $parser->parse_balanced_chunk( $wbxmlstring, $encoding ); |
| 217 |
|
| 218 |
This function parses a well balanced XML string into a L<<<<<< XML::LibXML::DocumentFragment >>>>>>. The first arguments contains the input string, the optional second argument |
| 219 |
can be used to specify character encoding of the input (UTF-8 is assumed by |
| 220 |
default). |
| 221 |
|
| 222 |
|
| 223 |
=item parse_xml_chunk |
| 224 |
|
| 225 |
This is the old name of parse_balanced_chunk(). Because it may causes confusion |
| 226 |
with the push parser interface, this function should not be used anymore. |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
=back |
| 231 |
|
| 232 |
By default XML::LibXML does not process XInclude tags within an XML Document |
| 233 |
(see options section below). XML::LibXML allows one to post-process a document |
| 234 |
to expand XInclude tags. |
| 235 |
|
| 236 |
=over 4 |
| 237 |
|
| 238 |
=item process_xincludes |
| 239 |
|
| 240 |
$parser->process_xincludes( $doc ); |
| 241 |
|
| 242 |
After a document is parsed into a DOM structure, you may want to expand the |
| 243 |
documents XInclude tags. This function processes the given document structure |
| 244 |
and expands all XInclude tags (or throws an error) by using the flags and |
| 245 |
callbacks of the given parser instance. |
| 246 |
|
| 247 |
Note that the resulting Tree contains some extra nodes (of type |
| 248 |
XML_XINCLUDE_START and XML_XINCLUDE_END) after successfully processing the |
| 249 |
document. These nodes indicate where data was included into the original tree. |
| 250 |
if the document is serialized, these extra nodes will not show up. |
| 251 |
|
| 252 |
Remember: A Document with processed XIncludes differs from the original |
| 253 |
document after serialization, because the original XInclude tags will not get |
| 254 |
restored! |
| 255 |
|
| 256 |
If the parser flag "expand_xincludes" is set to 1, you need not to post process |
| 257 |
the parsed document. |
| 258 |
|
| 259 |
|
| 260 |
=item processXIncludes |
| 261 |
|
| 262 |
$parser->processXIncludes( $doc ); |
| 263 |
|
| 264 |
This is an alias to process_xincludes, but through a JAVA like function name. |
| 265 |
|
| 266 |
|
| 267 |
=item parse_file |
| 268 |
|
| 269 |
$doc = $parser->parse_file( $xmlfilename ); |
| 270 |
|
| 271 |
This function parses an XML document from a file or network; $xmlfilename can |
| 272 |
be either a filename or an URL. Note that for parsing files, this function is |
| 273 |
the fastest choice, about 6-8 times faster then parse_fh(). |
| 274 |
|
| 275 |
|
| 276 |
=item parse_fh |
| 277 |
|
| 278 |
$doc = $parser->parse_fh( $io_fh ); |
| 279 |
|
| 280 |
parse_fh() parses a IOREF or a subclass of IO::Handle. |
| 281 |
|
| 282 |
Because the data comes from an open handle, libxml2's parser does not know |
| 283 |
about the base URI of the document. To set the base URI one should use |
| 284 |
parse_fh() as follows: |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
my $doc = $parser->parse_fh( $io_fh, $baseuri ); |
| 289 |
|
| 290 |
|
| 291 |
=item parse_string |
| 292 |
|
| 293 |
$doc = $parser->parse_string( $xmlstring); |
| 294 |
|
| 295 |
This function is similar to parse_fh(), but it parses an XML document that is |
| 296 |
available as a single string in memory, or alternatively as a reference to a |
| 297 |
scalar containing a string. Again, you can pass an optional base URI to the |
| 298 |
function. |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
my $doc = $parser->parse_string( $xmlstring, $baseuri ); |
| 303 |
my $doc = $parser->parse_string(\$xmlstring, $baseuri); |
| 304 |
|
| 305 |
|
| 306 |
=item parse_html_file |
| 307 |
|
| 308 |
$doc = $parser->parse_html_file( $htmlfile, \%opts ); |
| 309 |
|
| 310 |
Similar to parse_file() but parses HTML (strict) documents; $htmlfile can be |
| 311 |
filename or URL. |
| 312 |
|
| 313 |
An optional second argument can be used to pass some options to the HTML parser |
| 314 |
as a HASH reference. See options labeled with HTML in L<<<<<< Parser Options >>>>>>. |
| 315 |
|
| 316 |
|
| 317 |
=item parse_html_fh |
| 318 |
|
| 319 |
$doc = $parser->parse_html_fh( $io_fh, \%opts ); |
| 320 |
|
| 321 |
Similar to parse_fh() but parses HTML (strict) streams. |
| 322 |
|
| 323 |
An optional second argument can be used to pass some options to the HTML parser |
| 324 |
as a HASH reference. See options labeled with HTML in L<<<<<< Parser Options >>>>>>. |
| 325 |
|
| 326 |
Note: encoding option may not work correctly with this function in libxml2 < |
| 327 |
2.6.27 if the HTML file declares charset using a META tag. |
| 328 |
|
| 329 |
|
| 330 |
=item parse_html_string |
| 331 |
|
| 332 |
$doc = $parser->parse_html_string( $htmlstring, \%opts ); |
| 333 |
|
| 334 |
Similar to parse_string() but parses HTML (strict) strings. |
| 335 |
|
| 336 |
An optional second argument can be used to pass some options to the HTML parser |
| 337 |
as a HASH reference. See options labeled with HTML in L<<<<<< Parser Options >>>>>>. |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
=back |
| 342 |
|
| 343 |
|
| 344 |
=head2 Push Parser |
| 345 |
|
| 346 |
XML::LibXML provides a push parser interface. Rather than pulling the data from |
| 347 |
a given source the push parser waits for the data to be pushed into it. |
| 348 |
|
| 349 |
This allows one to parse large documents without waiting for the parser to |
| 350 |
finish. The interface is especially useful if a program needs to pre-process |
| 351 |
the incoming pieces of XML (e.g. to detect document boundaries). |
| 352 |
|
| 353 |
While XML::LibXML parse_*() functions force the data to be a well-formed XML, |
| 354 |
the push parser will take any arbitrary string that contains some XML data. The |
| 355 |
only requirement is that all the pushed strings are together a well formed |
| 356 |
document. With the push parser interface a program can interrupt the parsing |
| 357 |
process as required, where the parse_*() functions give not enough flexibility. |
| 358 |
|
| 359 |
Different to the pull parser implemented in parse_fh() or parse_file(), the |
| 360 |
push parser is not able to find out about the documents end itself. Thus the |
| 361 |
calling program needs to indicate explicitly when the parsing is done. |
| 362 |
|
| 363 |
In XML::LibXML this is done by a single function: |
| 364 |
|
| 365 |
=over 4 |
| 366 |
|
| 367 |
=item parse_chunk |
| 368 |
|
| 369 |
$parser->parse_chunk($string, $terminate); |
| 370 |
|
| 371 |
parse_chunk() tries to parse a given chunk of data, which isn't necessarily |
| 372 |
well balanced data. The function takes two parameters: The chunk of data as a |
| 373 |
string and optional a termination flag. If the termination flag is set to a |
| 374 |
true value (e.g. 1), the parsing will be stopped and the resulting document |
| 375 |
will be returned as the following example describes: |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
my $parser = XML::LibXML->new; |
| 380 |
for my $string ( "<", "foo", ' bar="hello world"', "/>") { |
| 381 |
$parser->parse_chunk( $string ); |
| 382 |
} |
| 383 |
my $doc = $parser->parse_chunk("", 1); # terminate the parsing |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
=back |
| 388 |
|
| 389 |
Internally XML::LibXML provides three functions that control the push parser |
| 390 |
process: |
| 391 |
|
| 392 |
=over 4 |
| 393 |
|
| 394 |
=item init_push |
| 395 |
|
| 396 |
$parser->init_push(); |
| 397 |
|
| 398 |
Initializes the push parser. |
| 399 |
|
| 400 |
|
| 401 |
=item push |
| 402 |
|
| 403 |
$parser->push(@data); |
| 404 |
|
| 405 |
This function pushes the data stored inside the array to libxml2's parser. Each |
| 406 |
entry in @data must be a normal scalar! This method can be called repeatedly. |
| 407 |
|
| 408 |
|
| 409 |
=item finish_push |
| 410 |
|
| 411 |
$doc = $parser->finish_push( $recover ); |
| 412 |
|
| 413 |
This function returns the result of the parsing process. If this function is |
| 414 |
called without a parameter it will complain about non well-formed documents. If |
| 415 |
$restore is 1, the push parser can be used to restore broken or non well formed |
| 416 |
(XML) documents as the following example shows: |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
eval { |
| 421 |
$parser->push( "<foo>", "bar" ); |
| 422 |
$doc = $parser->finish_push(); # will report broken XML |
| 423 |
}; |
| 424 |
if ( $@ ) { |
| 425 |
# ... |
| 426 |
} |
| 427 |
|
| 428 |
This can be annoying if the closing tag is missed by accident. The following |
| 429 |
code will restore the document: |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
eval { |
| 434 |
$parser->push( "<foo>", "bar" ); |
| 435 |
$doc = $parser->finish_push(1); # will return the data parsed |
| 436 |
# unless an error happened |
| 437 |
}; |
| 438 |
|
| 439 |
print $doc->toString(); # returns "<foo>bar</foo>" |
| 440 |
|
| 441 |
Of course finish_push() will return nothing if there was no data pushed to the |
| 442 |
parser before. |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
=back |
| 447 |
|
| 448 |
|
| 449 |
=head2 Pull Parser (Reader) |
| 450 |
|
| 451 |
XML::LibXML also provides a pull-parser interface similar to the XmlReader |
| 452 |
interface in .NET. This interface is almost streaming, and is usually faster |
| 453 |
and simpler to use than SAX. See L<<<<<< XML::LibXML::Reader >>>>>>. |
| 454 |
|
| 455 |
|
| 456 |
=head2 Direct SAX Parser |
| 457 |
|
| 458 |
XML::LibXML provides a direct SAX parser in the L<<<<<< XML::LibXML::SAX >>>>>> module. |
| 459 |
|
| 460 |
|
| 461 |
=head2 DOM based SAX Parser |
| 462 |
|
| 463 |
XML::LibXML also provides a DOM based SAX parser. The SAX parser is defined in |
| 464 |
the module XML::LibXML::SAX::Parser. As it is not a stream based parser, it |
| 465 |
parses documents into a DOM and traverses the DOM tree instead. |
| 466 |
|
| 467 |
The API of this parser is exactly the same as any other Perl SAX2 parser. See |
| 468 |
XML::SAX::Intro for details. |
| 469 |
|
| 470 |
Aside from the regular parsing methods, you can access the DOM tree traverser |
| 471 |
directly, using the generate() method: |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
my $doc = build_yourself_a_document(); |
| 476 |
my $saxparser = $XML::LibXML::SAX::Parser->new( ... ); |
| 477 |
$parser->generate( $doc ); |
| 478 |
|
| 479 |
This is useful for serializing DOM trees, for example that you might have done |
| 480 |
prior processing on, or that you have as a result of XSLT processing. |
| 481 |
|
| 482 |
I<<<<<< WARNING >>>>>> |
| 483 |
|
| 484 |
This is NOT a streaming SAX parser. As I said above, this parser reads the |
| 485 |
entire document into a DOM and serialises it. Some people couldn't read that in |
| 486 |
the paragraph above so I've added this warning. If you want a streaming SAX |
| 487 |
parser look at the L<<<<<< XML::LibXML::SAX >>>>>> man page |
| 488 |
|
| 489 |
|
| 490 |
=head1 SERIALIZATION |
| 491 |
|
| 492 |
XML::LibXML provides some functions to serialize nodes and documents. The |
| 493 |
serialization functions are described on the L<<<<<< XML::LibXML::Node >>>>>> manpage or the L<<<<<< XML::LibXML::Document >>>>>> manpage. XML::LibXML checks three global flags that alter the serialization |
| 494 |
process: |
| 495 |
|
| 496 |
|
| 497 |
=over 4 |
| 498 |
|
| 499 |
=item * |
| 500 |
|
| 501 |
skipXMLDeclaration |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
=item * |
| 506 |
|
| 507 |
skipDTD |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
=item * |
| 512 |
|
| 513 |
setTagCompression |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
=back |
| 518 |
|
| 519 |
of that three functions only setTagCompression is available for all |
| 520 |
serialization functions. |
| 521 |
|
| 522 |
Because XML::LibXML does these flags not itself, one has to define them locally |
| 523 |
as the following example shows: |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
local $XML::LibXML::skipXMLDeclaration = 1; |
| 528 |
local $XML::LibXML::skipDTD = 1; |
| 529 |
local $XML::LibXML::setTagCompression = 1; |
| 530 |
|
| 531 |
If skipXMLDeclaration is defined and not '0', the XML declaration is omitted |
| 532 |
during serialization. |
| 533 |
|
| 534 |
If skipDTD is defined and not '0', an existing DTD would not be serialized with |
| 535 |
the document. |
| 536 |
|
| 537 |
If setTagCompression is defined and not '0' empty tags are displayed as open |
| 538 |
and closing tags rather than the shortcut. For example the empty tag I<<<<<< foo >>>>>> will be rendered as I<<<<<< E<lt>fooE<gt>E<lt>/fooE<gt> >>>>>> rather than I<<<<<< E<lt>foo/E<gt> >>>>>>. |
| 539 |
|
| 540 |
|
| 541 |
=head1 PARSER OPTIONS |
| 542 |
|
| 543 |
Handling of libxml2 parser options has been unified and improved in XML::LibXML |
| 544 |
1.70. You can now set default options for a particular parser instance by |
| 545 |
passing them to the constructor as C<<<<<< XML::LibXML-E<gt>new({name=E<gt>value, ...}) >>>>>> or C<<<<<< XML::LibXML-E<gt>new(name=E<gt>value,...) >>>>>>. The options can be queried and changed using the following methods (pre-1.70 |
| 546 |
interfaces such as C<<<<<< $parser-E<gt>load_ext_dtd(0) >>>>>> also exist, see below): |
| 547 |
|
| 548 |
=over 4 |
| 549 |
|
| 550 |
=item option_exists |
| 551 |
|
| 552 |
$parser->option_exists($name); |
| 553 |
|
| 554 |
Returns 1 if the current XML::LibXML version supports the option C<<<<<< $name >>>>>>, otherwise returns 0 (note that this does not necessarily mean that the option |
| 555 |
is supported by the underlying libxml2 library). |
| 556 |
|
| 557 |
|
| 558 |
=item get_option |
| 559 |
|
| 560 |
$parser->get_option($name); |
| 561 |
|
| 562 |
Returns the current value of the parser option C<<<<<< $name >>>>>>. |
| 563 |
|
| 564 |
|
| 565 |
=item set_option |
| 566 |
|
| 567 |
$parser->set_option($name,$value); |
| 568 |
|
| 569 |
Sets option C<<<<<< $name >>>>>> to value C<<<<<< $value >>>>>>. |
| 570 |
|
| 571 |
|
| 572 |
=item set_options |
| 573 |
|
| 574 |
$parser->set_options({$name=>$value,...}); |
| 575 |
|
| 576 |
Sets multiple parsing options at once. |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
=back |
| 581 |
|
| 582 |
IMPORTANT NOTE: This documentation reflects the parser flags available in |
| 583 |
libxml2 2.7.3. Some options have no effect if an older version of libxml2 is |
| 584 |
used. |
| 585 |
|
| 586 |
Each of the flags listed below is labeled |
| 587 |
|
| 588 |
=over 4 |
| 589 |
|
| 590 |
=item /parser/ |
| 591 |
|
| 592 |
if it can be used with a C<<<<<< XML::LibXML >>>>>> parser object (i.e. passed to C<<<<<< XML::LibXML-E<gt>new >>>>>>, C<<<<<< XML::LibXML-E<gt>set_option >>>>>>, etc.) |
| 593 |
|
| 594 |
|
| 595 |
=item /html/ |
| 596 |
|
| 597 |
if it can be used passed to the C<<<<<< parse_html_* >>>>>> methods |
| 598 |
|
| 599 |
|
| 600 |
=item /reader/ |
| 601 |
|
| 602 |
if it can be used with the C<<<<<< XML::LibXML::Reader >>>>>>. |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
=back |
| 607 |
|
| 608 |
Unless specified otherwise, the default for boolean valued options is 0 |
| 609 |
(false). |
| 610 |
|
| 611 |
The available options are: |
| 612 |
|
| 613 |
=over 4 |
| 614 |
|
| 615 |
=item URI |
| 616 |
|
| 617 |
/parser, html, reader/ |
| 618 |
|
| 619 |
In case of parsing strings or file handles, XML::LibXML doesn't know about the |
| 620 |
base uri of the document. To make relative references such as XIncludes work, |
| 621 |
one has to set a base URI, that is then used for the parsed document. |
| 622 |
|
| 623 |
|
| 624 |
=item line_numbers |
| 625 |
|
| 626 |
/parser, html, reader/ |
| 627 |
|
| 628 |
If this option is activated, libxml2 will store the line number of each element |
| 629 |
node in the parsed document. The line number can be obtained using the C<<<<<< line_number() >>>>>> method of the C<<<<<< XML::LibXML::Node >>>>>> class (for non-element nodes this may report the line number of the containing |
| 630 |
element). The line numbers are also used for reporting positions of validation |
| 631 |
errors. |
| 632 |
|
| 633 |
IMPORTANT: Due to limitations in the libxml2 library line numbers greater than |
| 634 |
65535 will be returned as 65535. Unfortunately, this is a long and sad story, |
| 635 |
please see L<<<<<< http://bugzilla.gnome.org/show_bug.cgi?id=325533 >>>>>> for more details. |
| 636 |
|
| 637 |
|
| 638 |
=item encoding |
| 639 |
|
| 640 |
/html/ |
| 641 |
|
| 642 |
character encoding of the input |
| 643 |
|
| 644 |
|
| 645 |
=item recover |
| 646 |
|
| 647 |
/parser, html, reader/ |
| 648 |
|
| 649 |
recover from errors; possible values are 0, 1, and 2 |
| 650 |
|
| 651 |
A true value turns on recovery mode which allows one to parse broken XML or |
| 652 |
HTML data. The recovery mode allows the parser to return the successfully |
| 653 |
parsed portion of the input document. This is useful for almost well-formed |
| 654 |
documents, where for example a closing tag is missing somewhere. Still, |
| 655 |
XML::LibXML will only parse until the first fatal (non-recoverable) error |
| 656 |
occurs, reporting recoverable parsing errors as warnings. To suppress even |
| 657 |
these warnings, use recover=>2. |
| 658 |
|
| 659 |
Note that validation is switched off automatically in recovery mode. |
| 660 |
|
| 661 |
|
| 662 |
=item expand_entities |
| 663 |
|
| 664 |
/parser, reader/ |
| 665 |
|
| 666 |
substitute entities; possible values are 0 and 1; default is 1 |
| 667 |
|
| 668 |
Note that although this flag disables entity substitution, it does not prevent |
| 669 |
the parser from loading external entities; when substitution of an external |
| 670 |
entity is disabled, the entity will be represented in the document tree by an |
| 671 |
XML_ENTITY_REF_NODE node whose subtree will be the content obtained by parsing |
| 672 |
the external resource; Although this nesting is visible from the DOM it is |
| 673 |
transparent to XPath data model, so it is possible to match nodes in an |
| 674 |
unexpanded entity by the same XPath expression as if the entity were expanded. |
| 675 |
See also ext_ent_handler. |
| 676 |
|
| 677 |
|
| 678 |
=item ext_ent_handler |
| 679 |
|
| 680 |
/parser/ |
| 681 |
|
| 682 |
Provide a custom external entity handler to be used when expand_entities is set |
| 683 |
to 1. Possible value is a subroutine reference. |
| 684 |
|
| 685 |
This feature does not work properly in libxml2 < 2.6.27! |
| 686 |
|
| 687 |
The subroutine provided is called whenever the parser needs to retrieve the |
| 688 |
content of an external entity. It is called with two arguments: the system ID |
| 689 |
(URI) and the public ID. The value returned by the subroutine is parsed as the |
| 690 |
content of the entity. |
| 691 |
|
| 692 |
This method can be used to completely disable entity loading, e.g. to prevent |
| 693 |
exploits of the type described at (L<<<<<< http://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 |
| 694 |
remote file (RSS feed) that contains an entity reference to a local file (e.g. C<<<<<< /etc/fstab >>>>>>). |
| 695 |
|
| 696 |
A more granular solution to this problem, however, is provided by custom URL |
| 697 |
resolvers, as in |
| 698 |
|
| 699 |
my $c = XML::LibXML::InputCallback->new(); |
| 700 |
sub match { # accept file:/ URIs except for XML catalogs in /etc/xml/ |
| 701 |
my ($uri) = @_; |
| 702 |
return ($uri=~m{^file:/} |
| 703 |
and $uri !~ m{^file:///etc/xml/}) |
| 704 |
? 1 : 0; |
| 705 |
} |
| 706 |
$c->register_callbacks([ \&match, sub{}, sub{}, sub{} ]); |
| 707 |
$parser->input_callbacks($c); |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
=item load_ext_dtd |
| 713 |
|
| 714 |
/parser, reader/ |
| 715 |
|
| 716 |
load the external DTD subset while parsing; possible values are 0 and 1. Unless |
| 717 |
specified, XML::LibXML sets this option to 1. |
| 718 |
|
| 719 |
This flag is also required for DTD Validation, to provide complete attribute, |
| 720 |
and to expand entities, regardless if the document has an internal subset. Thus |
| 721 |
switching off external DTD loading, will disable entity expansion, validation, |
| 722 |
and complete attributes on internal subsets as well. |
| 723 |
|
| 724 |
|
| 725 |
=item complete_attributes |
| 726 |
|
| 727 |
/parser, reader/ |
| 728 |
|
| 729 |
create default DTD attributes; possible values are 0 and 1 |
| 730 |
|
| 731 |
|
| 732 |
=item validation |
| 733 |
|
| 734 |
/parser, reader/ |
| 735 |
|
| 736 |
validate with the DTD; possible values are 0 and 1 |
| 737 |
|
| 738 |
|
| 739 |
=item suppress_errors |
| 740 |
|
| 741 |
/parser, html, reader/ |
| 742 |
|
| 743 |
suppress error reports; possible values are 0 and 1 |
| 744 |
|
| 745 |
|
| 746 |
=item suppress_warnings |
| 747 |
|
| 748 |
/parser, html, reader/ |
| 749 |
|
| 750 |
suppress warning reports; possible values are 0 and 1 |
| 751 |
|
| 752 |
|
| 753 |
=item pedantic_parser |
| 754 |
|
| 755 |
/parser, html, reader/ |
| 756 |
|
| 757 |
pedantic error reporting; possible values are 0 and 1 |
| 758 |
|
| 759 |
|
| 760 |
=item no_blanks |
| 761 |
|
| 762 |
/parser, html, reader/ |
| 763 |
|
| 764 |
remove blank nodes; possible values are 0 and 1 |
| 765 |
|
| 766 |
|
| 767 |
=item no_defdtd |
| 768 |
|
| 769 |
/html/ |
| 770 |
|
| 771 |
do not add a default DOCTYPE; possible values are 0 and 1 |
| 772 |
|
| 773 |
the default is (0) to add a DTD when the input html lacks one |
| 774 |
|
| 775 |
|
| 776 |
=item expand_xinclude or xinclude |
| 777 |
|
| 778 |
/parser, reader/ |
| 779 |
|
| 780 |
Implement XInclude substitution; possible values are 0 and 1 |
| 781 |
|
| 782 |
Expands XInclude tags immediately while parsing the document. Note that the |
| 783 |
parser will use the URI resolvers installed via C<<<<<< XML::LibXML::InputCallback >>>>>> to parse the included document (if any). |
| 784 |
|
| 785 |
|
| 786 |
=item no_xinclude_nodes |
| 787 |
|
| 788 |
/parser, reader/ |
| 789 |
|
| 790 |
do not generate XINCLUDE START/END nodes; possible values are 0 and 1 |
| 791 |
|
| 792 |
|
| 793 |
=item no_network |
| 794 |
|
| 795 |
/parser, html, reader/ |
| 796 |
|
| 797 |
Forbid network access; possible values are 0 and 1 |
| 798 |
|
| 799 |
If set to true, all attempts to fetch non-local resources (such as DTD or |
| 800 |
external entities) will fail (unless custom callbacks are defined). |
| 801 |
|
| 802 |
It may be necessary to use the flag C<<<<<< recover >>>>>> for processing documents requiring such resources while networking is off. |
| 803 |
|
| 804 |
|
| 805 |
=item clean_namespaces |
| 806 |
|
| 807 |
/parser, reader/ |
| 808 |
|
| 809 |
remove redundant namespaces declarations during parsing; possible values are 0 |
| 810 |
and 1. |
| 811 |
|
| 812 |
|
| 813 |
=item no_cdata |
| 814 |
|
| 815 |
/parser, html, reader/ |
| 816 |
|
| 817 |
merge CDATA as text nodes; possible values are 0 and 1 |
| 818 |
|
| 819 |
|
| 820 |
=item no_basefix |
| 821 |
|
| 822 |
/parser, reader/ |
| 823 |
|
| 824 |
not fixup XINCLUDE xml#base URIS; possible values are 0 and 1 |
| 825 |
|
| 826 |
|
| 827 |
=item huge |
| 828 |
|
| 829 |
/parser, html, reader/ |
| 830 |
|
| 831 |
relax any hardcoded limit from the parser; possible values are 0 and 1. Unless |
| 832 |
specified, XML::LibXML sets this option to 0. |
| 833 |
|
| 834 |
Note: the default value for this option was changed to protect against denial |
| 835 |
of service through entity expansion attacks. Before enabling the option ensure |
| 836 |
you have taken alternative measures to protect your application against this |
| 837 |
type of attack. |
| 838 |
|
| 839 |
|
| 840 |
=item gdome |
| 841 |
|
| 842 |
/parser/ |
| 843 |
|
| 844 |
THIS OPTION IS EXPERIMENTAL! |
| 845 |
|
| 846 |
Although quite powerful, XML::LibXML's DOM implementation is incomplete with |
| 847 |
respect to the DOM level 2 or level 3 specifications. XML::GDOME is based on |
| 848 |
libxml2 as well, and provides a rather complete DOM implementation by wrapping |
| 849 |
libgdome. This flag allows you to make use of XML::LibXML's full parser options |
| 850 |
and XML::GDOME's DOM implementation at the same time. |
| 851 |
|
| 852 |
To make use of this function, one has to install libgdome and configure |
| 853 |
XML::LibXML to use this library. For this you need to rebuild XML::LibXML! |
| 854 |
|
| 855 |
Note: this feature was not seriously tested in recent XML::LibXML releases. |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
=back |
| 860 |
|
| 861 |
For compatibility with XML::LibXML versions prior to 1.70, the following |
| 862 |
methods are also supported for querying and setting the corresponding parser |
| 863 |
options (if called without arguments, the methods return the current value of |
| 864 |
the corresponding parser options; with an argument sets the option to a given |
| 865 |
value): |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
$parser->validation(); |
| 870 |
$parser->recover(); |
| 871 |
$parser->pedantic_parser(); |
| 872 |
$parser->line_numbers(); |
| 873 |
$parser->load_ext_dtd(); |
| 874 |
$parser->complete_attributes(); |
| 875 |
$parser->expand_xinclude(); |
| 876 |
$parser->gdome_dom(); |
| 877 |
$parser->clean_namespaces(); |
| 878 |
$parser->no_network(); |
| 879 |
|
| 880 |
The following obsolete methods trigger parser options in some special way: |
| 881 |
|
| 882 |
=over 4 |
| 883 |
|
| 884 |
=item recover_silently |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
$parser->recover_silently(1); |
| 889 |
|
| 890 |
If called without an argument, returns true if the current value of the C<<<<<< recover >>>>>> parser option is 2 and returns false otherwise. With a true argument sets the C<<<<<< recover >>>>>> parser option to 2; with a false argument sets the C<<<<<< recover >>>>>> parser option to 0. |
| 891 |
|
| 892 |
|
| 893 |
=item expand_entities |
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
$parser->expand_entities(0); |
| 898 |
|
| 899 |
Get/set the C<<<<<< expand_entities >>>>>> option. If called with a true argument, also turns the C<<<<<< load_ext_dtd >>>>>> option to 1. |
| 900 |
|
| 901 |
|
| 902 |
=item keep_blanks |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
$parser->keep_blanks(0); |
| 907 |
|
| 908 |
This is actually the opposite of the C<<<<<< no_blanks >>>>>> parser option. If used without an argument retrieves negated value of C<<<<<< no_blanks >>>>>>. If used with an argument sets C<<<<<< no_blanks >>>>>> to the opposite value. |
| 909 |
|
| 910 |
|
| 911 |
=item base_uri |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
$parser->base_uri( $your_base_uri ); |
| 916 |
|
| 917 |
Get/set the C<<<<<< URI >>>>>> option. |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
=back |
| 922 |
|
| 923 |
|
| 924 |
=head1 XML CATALOGS |
| 925 |
|
| 926 |
C<<<<<< libxml2 >>>>>> supports XML catalogs. Catalogs are used to map remote resources to their local |
| 927 |
copies. Using catalogs can speed up parsing processes if many external |
| 928 |
resources from remote addresses are loaded into the parsed documents (such as |
| 929 |
DTDs or XIncludes). |
| 930 |
|
| 931 |
Note that libxml2 has a global pool of loaded catalogs, so if you apply the |
| 932 |
method C<<<<<< load_catalog >>>>>> to one parser instance, all parser instances will start using the catalog (in |
| 933 |
addition to other previously loaded catalogs). |
| 934 |
|
| 935 |
Note also that catalogs are not used when a custom external entity handler is |
| 936 |
specified. At the current state it is not possible to make use of both types of |
| 937 |
resolving systems at the same time. |
| 938 |
|
| 939 |
=over 4 |
| 940 |
|
| 941 |
=item load_catalog |
| 942 |
|
| 943 |
$parser->load_catalog( $catalog_file ); |
| 944 |
|
| 945 |
Loads the XML catalog file $catalog_file. |
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
# Global external entity loader (similar to ext_ent_handler option |
| 950 |
# but this works really globally, also in XML::LibXSLT include etc..) |
| 951 |
|
| 952 |
XML::LibXML::externalEntityLoader(\&my_loader); |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
=back |
| 957 |
|
| 958 |
|
| 959 |
=head1 ERROR REPORTING |
| 960 |
|
| 961 |
XML::LibXML throws exceptions during parsing, validation or XPath processing |
| 962 |
(and some other occasions). These errors can be caught by using I<<<<<< eval >>>>>> blocks. The error is stored in I<<<<<< $@ >>>>>>. There are two implementations: the old one throws $@ which is just a message |
| 963 |
string, in the new one $@ is an object from the class XML::LibXML::Error; this |
| 964 |
class overrides the operator "" so that when printed, the object flattens to |
| 965 |
the usual error message. |
| 966 |
|
| 967 |
XML::LibXML throws errors as they occur. This is a very common misunderstanding |
| 968 |
in the use of XML::LibXML. If the eval is omitted, XML::LibXML will always halt |
| 969 |
your script by "croaking" (see Carp man page for details). |
| 970 |
|
| 971 |
Also note that an increasing number of functions throw errors if bad data is |
| 972 |
passed as arguments. If you cannot assure valid data passed to XML::LibXML you |
| 973 |
should eval these functions. |
| 974 |
|
| 975 |
Note: since version 1.59, get_last_error() is no longer available in |
| 976 |
XML::LibXML for thread-safety reasons. |
| 977 |
|
| 978 |
=head1 AUTHORS |
| 979 |
|
| 980 |
Matt Sergeant, |
| 981 |
Christian Glahn, |
| 982 |
Petr Pajas |
| 983 |
|
| 984 |
|
| 985 |
=head1 VERSION |
| 986 |
|
| 987 |
2.0134 |
| 988 |
|
| 989 |
=head1 COPYRIGHT |
| 990 |
|
| 991 |
2001-2007, AxKit.com Ltd. |
| 992 |
|
| 993 |
2002-2006, Christian Glahn. |
| 994 |
|
| 995 |
2006-2009, Petr Pajas. |
| 996 |
|
| 997 |
=cut |
| 998 |
|
| 999 |
|
| 1000 |
=head1 LICENSE |
| 1001 |
|
| 1002 |
This program is free software; you can redistribute it and/or modify it under |
| 1003 |
the same terms as Perl itself. |
| 1004 |
|