Debian Codegraph @

lib/XML/LibXML/Reader.pod

raw
1 =head1 NAME
2
3 XML::LibXML::Reader - XML::LibXML::Reader - interface to libxml2 pull parser
4
5 =head1 SYNOPSIS
6
7
8
9 use XML::LibXML::Reader;
10
11
12
13 my $reader = XML::LibXML::Reader->new(location => "file.xml")
14 or die "cannot read file.xml\n";
15 while ($reader->read) {
16 processNode($reader);
17 }
18
19
20
21 sub processNode {
22 my $reader = shift;
23 printf "%d %d %s %d\n", ($reader->depth,
24 $reader->nodeType,
25 $reader->name,
26 $reader->isEmptyElement);
27 }
28
29 or
30
31
32
33 my $reader = XML::LibXML::Reader->new(location => "file.xml")
34 or die "cannot read file.xml\n";
35 $reader->preservePattern('//table/tr');
36 $reader->finish;
37 print $reader->document->toString(1);
38
39
40 =head1 DESCRIPTION
41
42 This is a perl interface to libxml2's pull-parser implementation xmlTextReader I<<<<<< http://xmlsoft.org/html/libxml-xmlreader.html >>>>>>. This feature requires at least libxml2-2.6.21. Pull-parsers (such as StAX in
43 Java, or XmlReader in C#) use an iterator approach to parse XML documents. They
44 are easier to program than event-based parser (SAX) and much more lightweight
45 than tree-based parser (DOM), which load the complete tree into memory.
46
47 The Reader acts as a cursor going forward on the document stream and stopping
48 at each node on the way. At every point, the DOM-like methods of the Reader
49 object allow one to examine the current node (name, namespace, attributes,
50 etc.)
51
52 The user's code keeps control of the progress and simply calls the C<<<<<< read() >>>>>> function repeatedly to progress to the next node in the document order. Other
53 functions provide means for skipping complete sub-trees, or nodes until a
54 specific element, etc.
55
56 At every time, only a very limited portion of the document is kept in the
57 memory, which makes the API more memory-efficient than using DOM. However, it
58 is also possible to mix Reader with DOM. At every point the user may copy the
59 current node (optionally expanded into a complete sub-tree) from the processed
60 document to another DOM tree, or to instruct the Reader to collect sub-document
61 in form of a DOM tree consisting of selected nodes.
62
63 Reader API also supports namespaces, xml:base, entity handling, and DTD
64 validation. Schema and RelaxNG validation support will probably be added in
65 some later revision of the Perl interface.
66
67 The naming of methods compared to libxml2 and C# XmlTextReader has been changed
68 slightly to match the conventions of XML::LibXML. Some functions have been
69 changed or added with respect to the C interface.
70
71
72 =head1 CONSTRUCTOR
73
74 Depending on the XML source, the Reader object can be created with either of:
75
76
77
78 my $reader = XML::LibXML::Reader->new( location => "file.xml", ... );
79 my $reader = XML::LibXML::Reader->new( string => $xml_string, ... );
80 my $reader = XML::LibXML::Reader->new( IO => $file_handle, ... );
81 my $reader = XML::LibXML::Reader->new( FD => fileno(STDIN), ... );
82 my $reader = XML::LibXML::Reader->new( DOM => $dom, ... );
83
84 where ... are (optional) reader options described below in L<<<<<< Reader options >>>>>> or various parser options described in L<<<<<< XML::LibXML::Parser >>>>>>. The constructor recognizes the following XML sources:
85
86
87 =head2 Source specification
88
89 =over 4
90
91 =item location
92
93 Read XML from a local file or URL.
94
95
96 =item string
97
98 Read XML from a string.
99
100
101 =item IO
102
103 Read XML a Perl IO filehandle.
104
105
106 =item FD
107
108 Read XML from a file descriptor (bypasses Perl I/O layer, only applicable to
109 filehandles for regular files or pipes). Possibly faster than IO.
110
111
112 =item DOM
113
114 Use reader API to walk through a pre-parsed L<<<<<< XML::LibXML::Document >>>>>>.
115
116
117
118 =back
119
120
121 =head2 Reader options
122
123 =over 4
124
125 =item encoding => $encoding
126
127 override document encoding.
128
129
130 =item RelaxNG => $rng_schema
131
132 can be used to pass either a L<<<<<< XML::LibXML::RelaxNG >>>>>> object or a filename or URL of a RelaxNG schema to the constructor. The schema
133 is then used to validate the document as it is processed.
134
135
136 =item Schema => $xsd_schema
137
138 can be used to pass either a L<<<<<< XML::LibXML::Schema >>>>>> object or a filename or URL of a W3C XSD schema to the constructor. The schema
139 is then used to validate the document as it is processed.
140
141
142 =item ...
143
144 the reader further supports various parser options described in L<<<<<< XML::LibXML::Parser >>>>>> (specifically those labeled by /reader/).
145
146
147
148 =back
149
150
151 =head1 METHODS CONTROLLING PARSING PROGRESS
152
153 =over 4
154
155 =item read ()
156
157 Moves the position to the next node in the stream, exposing its properties.
158
159 Returns 1 if the node was read successfully, 0 if there is no more nodes to
160 read, or -1 in case of error
161
162
163 =item readAttributeValue ()
164
165 Parses an attribute value into one or more Text and EntityReference nodes.
166
167 Returns 1 in case of success, 0 if the reader was not positioned on an
168 attribute node or all the attribute values have been read, or -1 in case of
169 error.
170
171
172 =item readState ()
173
174 Gets the read state of the reader. Returns the state value, or -1 in case of
175 error. The module exports constants for the Reader states, see STATES below.
176
177
178 =item depth ()
179
180 The depth of the node in the tree, starts at 0 for the root node.
181
182
183 =item next ()
184
185 Skip to the node following the current one in the document order while avoiding
186 the sub-tree if any. Returns 1 if the node was read successfully, 0 if there is
187 no more nodes to read, or -1 in case of error.
188
189
190 =item nextElement (localname?,nsURI?)
191
192 Skip nodes following the current one in the document order until a specific
193 element is reached. The element's name must be equal to a given localname if
194 defined, and its namespace must equal to a given nsURI if defined. Either of
195 the arguments can be undefined (or omitted, in case of the latter or both).
196
197 Returns 1 if the element was found, 0 if there is no more nodes to read, or -1
198 in case of error.
199
200
201 =item nextPatternMatch (compiled_pattern)
202
203 Skip nodes following the current one in the document order until an element
204 matching a given compiled pattern is reached. See L<<<<<< XML::LibXML::Pattern >>>>>> for information on compiled patterns. See also the C<<<<<< matchesPattern >>>>>> method.
205
206 Returns 1 if the element was found, 0 if there is no more nodes to read, or -1
207 in case of error.
208
209
210 =item skipSiblings ()
211
212 Skip all nodes on the same or lower level until the first node on a higher
213 level is reached. In particular, if the current node occurs in an element, the
214 reader stops at the end tag of the parent element, otherwise it stops at a node
215 immediately following the parent node.
216
217 Returns 1 if successful, 0 if end of the document is reached, or -1 in case of
218 error.
219
220
221 =item nextSibling ()
222
223 It skips to the node following the current one in the document order while
224 avoiding the sub-tree if any.
225
226 Returns 1 if the node was read successfully, 0 if there is no more nodes to
227 read, or -1 in case of error
228
229
230 =item nextSiblingElement (name?,nsURI?)
231
232 Like nextElement but only processes sibling elements of the current node
233 (moving forward using C<<<<<< nextSibling () >>>>>> rather than C<<<<<< read () >>>>>>, internally).
234
235 Returns 1 if the element was found, 0 if there is no more sibling nodes, or -1
236 in case of error.
237
238
239 =item finish ()
240
241 Skip all remaining nodes in the document, reaching end of the document.
242
243 Returns 1 if successful, 0 in case of error.
244
245
246 =item close ()
247
248 This method releases any resources allocated by the current instance and closes
249 any underlying input. It returns 0 on failure and 1 on success. This method is
250 automatically called by the destructor when the reader is forgotten, therefore
251 you do not have to call it directly.
252
253
254
255 =back
256
257
258 =head1 METHODS EXTRACTING INFORMATION
259
260 =over 4
261
262 =item name ()
263
264 Returns the qualified name of the current node, equal to (Prefix:)LocalName.
265
266
267 =item nodeType ()
268
269 Returns the type of the current node. See NODE TYPES below.
270
271
272 =item localName ()
273
274 Returns the local name of the node.
275
276
277 =item prefix ()
278
279 Returns the prefix of the namespace associated with the node.
280
281
282 =item namespaceURI ()
283
284 Returns the URI defining the namespace associated with the node.
285
286
287 =item isEmptyElement ()
288
289 Check if the current node is empty, this is a bit bizarre in the sense that
290 <a/> will be considered empty while <a></a> will not.
291
292
293 =item hasValue ()
294
295 Returns true if the node can have a text value.
296
297
298 =item value ()
299
300 Provides the text value of the node if present or undef if not available.
301
302
303 =item readInnerXml ()
304
305 Reads the contents of the current node, including child nodes and markup.
306 Returns a string containing the XML of the node's content, or undef if the
307 current node is neither an element nor attribute, or has no child nodes.
308
309
310 =item readOuterXml ()
311
312 Reads the contents of the current node, including child nodes and markup.
313
314 Returns a string containing the XML of the node including its content, or undef
315 if the current node is neither an element nor attribute.
316
317
318 =item nodePath()
319
320 Returns a canonical location path to the current element from the root node to
321 the current node. Namespaced elements are matched by '*', because there is no
322 way to declare prefixes within XPath patterns. Unlike C<<<<<< XML::LibXML::Node::nodePath() >>>>>>, this function does not provide sibling counts (i.e. instead of e.g. '/a/b[1]'
323 and '/a/b[2]' you get '/a/b' for both matches).
324
325
326 =item matchesPattern(compiled_pattern)
327
328 Returns a true value if the current node matches a compiled pattern. See L<<<<<< XML::LibXML::Pattern >>>>>> for information on compiled patterns. See also the C<<<<<< nextPatternMatch >>>>>> method.
329
330
331
332 =back
333
334
335 =head1 METHODS EXTRACTING DOM NODES
336
337 =over 4
338
339 =item document ()
340
341 Provides access to the document tree built by the reader. This function can be
342 used to collect the preserved nodes (see C<<<<<< preserveNode() >>>>>> and preservePattern).
343
344 CAUTION: Never use this function to modify the tree unless reading of the whole
345 document is completed!
346
347
348 =item copyCurrentNode (deep)
349
350 This function is similar a DOM function C<<<<<< copyNode() >>>>>>. It returns a copy of the currently processed node as a corresponding DOM
351 object. Use deep = 1 to obtain the full sub-tree.
352
353
354 =item preserveNode ()
355
356 This tells the XML Reader to preserve the current node in the document tree. A
357 document tree consisting of the preserved nodes and their content can be
358 obtained using the method C<<<<<< document() >>>>>> once parsing is finished.
359
360 Returns the node or NULL in case of error.
361
362
363 =item preservePattern (pattern,\%ns_map)
364
365 This tells the XML Reader to preserve all nodes matched by the pattern (which
366 is a streaming XPath subset). A document tree consisting of the preserved nodes
367 and their content can be obtained using the method C<<<<<< document() >>>>>> once parsing is finished.
368
369 An optional second argument can be used to provide a HASH reference mapping
370 prefixes used by the XPath to namespace URIs.
371
372 The XPath subset available with this function is described at
373
374
375
376 http://www.w3.org/TR/xmlschema-1/#Selector
377
378 and matches the production
379
380
381
382 Path ::= ('.//')? ( Step '/' )* ( Step | '@' NameTest )
383
384 Returns a positive number in case of success and -1 in case of error
385
386
387
388 =back
389
390
391 =head1 METHODS PROCESSING ATTRIBUTES
392
393 =over 4
394
395 =item attributeCount ()
396
397 Provides the number of attributes of the current node.
398
399
400 =item hasAttributes ()
401
402 Whether the node has attributes.
403
404
405 =item getAttribute (name)
406
407 Provides the value of the attribute with the specified qualified name.
408
409 Returns a string containing the value of the specified attribute, or undef in
410 case of error.
411
412
413 =item getAttributeNs (localName, namespaceURI)
414
415 Provides the value of the specified attribute.
416
417 Returns a string containing the value of the specified attribute, or undef in
418 case of error.
419
420
421 =item getAttributeNo (no)
422
423 Provides the value of the attribute with the specified index relative to the
424 containing element.
425
426 Returns a string containing the value of the specified attribute, or undef in
427 case of error.
428
429
430 =item isDefault ()
431
432 Returns true if the current attribute node was generated from the default value
433 defined in the DTD.
434
435
436 =item moveToAttribute (name)
437
438 Moves the position to the attribute with the specified local name and namespace
439 URI.
440
441 Returns 1 in case of success, -1 in case of error, 0 if not found
442
443
444 =item moveToAttributeNo (no)
445
446 Moves the position to the attribute with the specified index relative to the
447 containing element.
448
449 Returns 1 in case of success, -1 in case of error, 0 if not found
450
451
452 =item moveToAttributeNs (localName,namespaceURI)
453
454 Moves the position to the attribute with the specified local name and namespace
455 URI.
456
457 Returns 1 in case of success, -1 in case of error, 0 if not found
458
459
460 =item moveToFirstAttribute ()
461
462 Moves the position to the first attribute associated with the current node.
463
464 Returns 1 in case of success, -1 in case of error, 0 if not found
465
466
467 =item moveToNextAttribute ()
468
469 Moves the position to the next attribute associated with the current node.
470
471 Returns 1 in case of success, -1 in case of error, 0 if not found
472
473
474 =item moveToElement ()
475
476 Moves the position to the node that contains the current attribute node.
477
478 Returns 1 in case of success, -1 in case of error, 0 if not moved
479
480
481 =item isNamespaceDecl ()
482
483 Determine whether the current node is a namespace declaration rather than a
484 regular attribute.
485
486 Returns 1 if the current node is a namespace declaration, 0 if it is a regular
487 attribute or other type of node, or -1 in case of error.
488
489
490
491 =back
492
493
494 =head1 OTHER METHODS
495
496 =over 4
497
498 =item lookupNamespace (prefix)
499
500 Resolves a namespace prefix in the scope of the current element.
501
502 Returns a string containing the namespace URI to which the prefix maps or undef
503 in case of error.
504
505
506 =item encoding ()
507
508 Returns a string containing the encoding of the document or undef in case of
509 error.
510
511
512 =item standalone ()
513
514 Determine the standalone status of the document being read. Returns 1 if the
515 document was declared to be standalone, 0 if it was declared to be not
516 standalone, or -1 if the document did not specify its standalone status or in
517 case of error.
518
519
520 =item xmlVersion ()
521
522 Determine the XML version of the document being read. Returns a string
523 containing the XML version of the document or undef in case of error.
524
525
526 =item baseURI ()
527
528 Returns the base URI of a given node.
529
530
531 =item isValid ()
532
533 Retrieve the validity status from the parser.
534
535 Returns 1 if valid, 0 if no, and -1 in case of error.
536
537
538 =item xmlLang ()
539
540 The xml:lang scope within which the node resides.
541
542
543 =item lineNumber ()
544
545 Provide the line number of the current parsing point.
546
547
548 =item columnNumber ()
549
550 Provide the column number of the current parsing point.
551
552
553 =item byteConsumed ()
554
555 This function provides the current index of the parser relative to the start of
556 the current entity. This function is computed in bytes from the beginning
557 starting at zero and finishing at the size in bytes of the file if parsing a
558 file. The function is of constant cost if the input is UTF-8 but can be costly
559 if run on non-UTF-8 input.
560
561
562 =item setParserProp (prop => value, ...)
563
564 Change the parser processing behaviour by changing some of its internal
565 properties. The following properties are available with this function:
566 ``load_ext_dtd'', ``complete_attributes'', ``validation'', ``expand_entities''.
567
568 Since some of the properties can only be changed before any read has been done,
569 it is best to set the parsing properties at the constructor.
570
571 Returns 0 if the call was successful, or -1 in case of error
572
573
574 =item getParserProp (prop)
575
576 Get value of an parser internal property. The following property names can be
577 used: ``load_ext_dtd'', ``complete_attributes'', ``validation'',
578 ``expand_entities''.
579
580 Returns the value, usually 0 or 1, or -1 in case of error.
581
582
583
584 =back
585
586
587 =head1 DESTRUCTION
588
589 XML::LibXML takes care of the reader object destruction when the last reference
590 to the reader object goes out of scope. The document tree is preserved, though,
591 if either of $reader->document or $reader->preserveNode was used and references
592 to the document tree exist.
593
594
595 =head1 NODE TYPES
596
597 The reader interface provides the following constants for node types (the
598 constant symbols are exported by default or if tag C<<<<<< :types >>>>>> is used).
599
600
601
602 XML_READER_TYPE_NONE => 0
603 XML_READER_TYPE_ELEMENT => 1
604 XML_READER_TYPE_ATTRIBUTE => 2
605 XML_READER_TYPE_TEXT => 3
606 XML_READER_TYPE_CDATA => 4
607 XML_READER_TYPE_ENTITY_REFERENCE => 5
608 XML_READER_TYPE_ENTITY => 6
609 XML_READER_TYPE_PROCESSING_INSTRUCTION => 7
610 XML_READER_TYPE_COMMENT => 8
611 XML_READER_TYPE_DOCUMENT => 9
612 XML_READER_TYPE_DOCUMENT_TYPE => 10
613 XML_READER_TYPE_DOCUMENT_FRAGMENT => 11
614 XML_READER_TYPE_NOTATION => 12
615 XML_READER_TYPE_WHITESPACE => 13
616 XML_READER_TYPE_SIGNIFICANT_WHITESPACE => 14
617 XML_READER_TYPE_END_ELEMENT => 15
618 XML_READER_TYPE_END_ENTITY => 16
619 XML_READER_TYPE_XML_DECLARATION => 17
620
621
622 =head1 STATES
623
624 The following constants represent the values returned by C<<<<<< readState() >>>>>>. They are exported by default, or if tag C<<<<<< :states >>>>>> is used:
625
626
627
628 XML_READER_NONE => -1
629 XML_READER_START => 0
630 XML_READER_ELEMENT => 1
631 XML_READER_END => 2
632 XML_READER_EMPTY => 3
633 XML_READER_BACKTRACK => 4
634 XML_READER_DONE => 5
635 XML_READER_ERROR => 6
636
637
638 =head1 SEE ALSO
639
640 L<<<<<< XML::LibXML::Pattern >>>>>> for information about compiled patterns.
641
642 http://xmlsoft.org/html/libxml-xmlreader.html
643
644 http://dotgnu.org/pnetlib-doc/System/Xml/XmlTextReader.html
645
646
647 =head1 ORIGINAL IMPLEMENTATION
648
649 Heiko Klein, <[email protected]<gt> and Petr Pajas
650
651 =head1 AUTHORS
652
653 Matt Sergeant,
654 Christian Glahn,
655 Petr Pajas
656
657
658 =head1 VERSION
659
660 2.0134
661
662 =head1 COPYRIGHT
663
664 2001-2007, AxKit.com Ltd.
665
666 2002-2006, Christian Glahn.
667
668 2006-2009, Petr Pajas.
669
670 =cut
671
672
673 =head1 LICENSE
674
675 This program is free software; you can redistribute it and/or modify it under
676 the same terms as Perl itself.
677