TokeParser(3pm) | User Contributed Perl Documentation | TokeParser(3pm) |
XML::TokeParser - Simplified interface to XML::Parser
use XML::TokeParser; # #parse from file my $p = XML::TokeParser->new('file.xml') # #parse from open handle open IN, 'file.xml' or die $!; my $p = XML::TokeParser->new( \*IN, Noempty => 1 ); # #parse literal text my $text = '<tag xmlns="http://www.omsdev.com">text</tag>'; my $p = XML::TokeParser->new( \$text, Namespaces => 1 ); # #read next token my $token = $p->get_token(); # #skip to <title> and read text $p->get_tag('title'); $p->get_text(); # #read text of next <para>, ignoring any internal markup $p->get_tag('para'); $p->get_trimmed_text('/para'); # #process <para> if interesting text $t = $p->get_tag('para'); $p->begin_saving($t); if ( $p->get_trimmed_text('/para') =~ /interesting stuff/ ) { $p->restore_saved(); process_para($p); }
XML::TokeParser provides a procedural ("pull mode") interface to XML::Parser in much the same way that Gisle Aas' HTML::TokeParser provides a procedural interface to HTML::Parser. XML::TokeParser splits its XML input up into "tokens," each corresponding to an XML::Parser event.
A token is a bless'd reference to an array whose first element is an event-type string and whose last element is the literal text of the XML input that generated the event, with intermediate elements varying according to the event type.
Each token is an object of type XML::TokeParser::Token. Read "XML::TokeParser::Token" to learn what methods are available for inspecting the token, and retrieving data from it.
Options are name=>value pairs and can be any of the following:
A token is a blessed array reference, that you acquire using "$p->get_token" or "$p->get_tag", and that might look like:
["S", $tag, $attr, $attrseq, $raw] ["E", $tag, $raw] ["T", $text, $raw] ["C", $text, $raw] ["PI", $target, $data, $raw]
If you don't like remembering array indices (you're a real programmer), you may access the attributes of a token like:
"$t->tag", "$t->attr", "$t->attrseq", "$t->raw", "$t->text", "$t->target", "$t->data".
****Please note that this may change in the future, where as there will be 4 token types, XML::TokeParser::Token::StartTag ....
What kind of token is it?
To find out, inspect your token using any of these is_* methods (1 == true, 0 == false, d'oh):
What's that token made of? To retrieve data from your token, use any of the following methods, depending on the kind of token you have:
Here's more detailed info about the tokens.
The literal text includes any markup delimiters (pointy brackets, <![CDATA[, etc.), entity references, and numeric character references and is in the XML document's original character encoding. All other text is in UTF-8 (unless the Latin option is set, in which case it's in ISO-8859-1) regardless of the original encoding, and all entity and character references are expanded.
If the Namespaces option is set, element and attribute names are prefixed by their (possibly empty) namespace URIs enclosed in curly brackets and xmlns:* attributes do not appear in 'S' tokens.
Uses a true XML parser rather than a modified HTML parser.
Text and comment tokens include extracted text as well as literal text.
PI tokens include target and data as well as literal text.
No tokens for declarations.
No "textify" hash.
unget_token correctly handles partial tokens returned by get_tag().
begin_saving() and restore_saved()
Example:
use XML::TokeParser; use strict; # my $text = '<tag foo="bar" foy="floy"> some text <!--comment--></tag>'; my $p = XML::TokeParser->new( \$text ); # print $/; # while( defined( my $t = $p->get_token() ) ){ local $\="\n"; print ' raw = ', $t->raw; # if( $t->tag ){ print ' tag = ', $t->tag; # if( $t->is_start_tag ) { print ' attr = ', join ',', %{$t->attr}; print ' attrseq = ', join ',', @{$t->attrseq}; } # print 'is_tag ', $t->is_tag; print 'is_start_tag ', $t->is_start_tag; print 'is_end_tag ', $t->is_end_tag; } elsif( $t->is_pi ){ print ' target = ', $t->target; print ' data = ', $t->data; print 'is_pi ', $t->is_pi; } else { print ' text = ', $t->text; print 'is_text ', $t->is_text; print 'is_comment ', $t->is_comment; } # print $/; } __END__
Output:
raw = <tag foo="bar" foy="floy"> tag = tag attr = foo,bar,foy,floy attrseq = foo,foy is_tag 1 is_start_tag 1 is_end_tag 0 raw = some text text = some text is_text 1 is_comment 0 raw = <!--comment--> text = comment is_text 0 is_comment 1 raw = </tag> tag = tag is_tag 1 is_start_tag 0 is_end_tag 1
To report bugs, go to <http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-TokeParser> or send mail to <bug-XML-Tokeparser@rt.cpan.org>
Copyright (c) 2003 D.H. aka PodMaster (current maintainer). Copyright (c) 2001 Eric Bohlman (original author).
All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. If you don't know what this means, visit <http://perl.com/> or <http://cpan.org/>.
HTML::TokeParser, XML::Parser, XML::Catalog, XML::Smart, XML::Twig.
2003-06-09 | perl v5.10.1 |