1
<?xml version='1.0' encoding='iso-8859-1'?>
2
<pod xmlns="http://axkit.org/ns/2000/pod2xml">
4
<title>XML::TokeParser - Simplified interface to XML::Parser</title>
7
<title>SYNOPSIS</title>
13
my $p=XML::TokeParser->new('file.xml')
16
#parse from open handle
17
open IN,'file.xml' or die $!;
18
my $p=XML::TokeParser->new(\*IN,Noempty=>1);
22
my $text='<tag xmlns="http://www.omsdev.com">text</tag>';
23
my $p=XML::TokeParser->new(\$text,Namespaces=>1);
27
my $token=$p->get_token();
30
#skip to <title> and read text
35
#read text of next <para>, ignoring any internal markup
37
$p->get_trimmed_text('/para');
41
<title>DESCRIPTION</title>
43
XML::TokeParser provides a procedural ("pull mode") interface to XML::Parser
44
in much the same way that Gisle Aas' HTML::TokeParser provides a procedural
45
interface to HTML::Parser. XML::TokeParser splits its XML input up into
46
"tokens," each corresponding to an XML::Parser event.
49
A token is a reference to an array whose first element is an event-type
50
string and whose last element is the literal text of the XML input that
51
generated the event, with intermediate elements varying according to the
55
<item><itemtext>Start tag</itemtext>
57
The token has five elements: 'S', the element's name, a reference to a hash
58
of attribute values keyed by attribute names, a reference to an array of
59
attribute names in the order in which they appeared in the tag, and the
63
<item><itemtext>End tag</itemtext>
65
The token has three elements: 'E', the element's name, and the literal text.
68
<item><itemtext>Character data (text)</itemtext>
70
The token has three elements: 'T', the parsed text, and the literal text.
71
All contiguous runs of text are gathered into single tokens; there will
72
never be two 'T' tokens in a row.
75
<item><itemtext>Comment</itemtext>
77
The token has three elements: 'C', the parsed text of the comment, and the
81
<item><itemtext>Processing instruction</itemtext>
83
The token has four elements: 'PI', the target, the data, and the literal
89
The literal text includes any markup delimiters (pointy brackets,
90
<![CDATA[, etc.), entity references, and numeric character references and
91
is in the XML document's original character encoding. All other text is in
92
UTF-8 (unless the Latin option is set, in which case it's in ISO-8859-1)
93
regardless of the original encoding, and all entity and character
94
references are expanded.
97
If the Namespaces option is set, element and attribute names are prefixed
98
by their (possibly empty) namespace URIs enclosed in curly brackets and
99
xmlns:* attributes do not appear in 'S' tokens.
103
<title>METHODS</title>
105
<item><itemtext>$p = XML::TokeParser->new($input, [options])</itemtext>
107
Creates a new parser, specifying the input source and any options. If
108
$input is a string, it is the name of the file to parse. If $input is a
109
reference to a string, that string is the actual text to parse. If $input
110
is a reference to a typeglob or an IO::Handle object corresponding to an
111
open file or socket, the text read from the handle will be parsed.
114
Options are name=>value pairs and can be any of the following:
118
<item><itemtext>Namespaces</itemtext>
120
If set to a true value, namespace processing is enabled.
123
<item><itemtext>ParseParamEnt</itemtext>
125
This option is passed on to the underlying XML::Parser object; see that
126
module's documentation for details.
129
<item><itemtext>Noempty</itemtext>
131
If set to a true value, text tokens consisting of only whitespace (such as
132
those created by indentation and line breaks in between tags) will be
136
<item><itemtext>Latin</itemtext>
138
If set to a true value, all text other than the literal text elements of
139
tokens will be translated into the ISO 8859-1 (Latin-1) character encoding
140
rather than the normal UTF-8 encoding.
143
<item><itemtext>Catalog</itemtext>
145
The value is the URI of a catalog file used to resolve PUBLIC and SYSTEM
146
identifiers. See XML::Catalog for details.
150
<item><itemtext>$token = $p->get_token()</itemtext>
152
Returns the next token, as an array reference, from the input. Returns
153
undef if there are no remaining tokens.
156
<item><itemtext>$p->unget_token($token,...)</itemtext>
158
Pushes tokens back so they will be re-read. Useful if you've read one or
162
<item><itemtext>$token = $p->get_tag( [$token] )</itemtext>
164
If no argument given, skips tokens until the next start tag or end tag
165
token. If an argument is given, skips tokens until the start tag or end tag
166
(if the argument begins with '/') for the named element. The returned
167
token does not include an event type code; its first element is the element
168
name, prefixed by a '/' if the token is for an end tag.
171
<item><itemtext>$text = $p->get_text( [$token] )</itemtext>
173
If no argument given, returns the text at the current position, or an empty
174
string if the next token is not a 'T' token. If an argument is given,
175
gathers up all text between the current position and the specified start or
176
end tag, stripping out any intervening tags (much like the way a typical
177
Web browser deals with unknown tags).
180
<item><itemtext>$text = $p->get_trimmed_text( [$token])</itemtext>
182
Like get_text(), but deletes any leading or trailing whitespaces and
183
collapses multiple whitespace (including newlines) into single spaces.
189
<title>DIFFERENCES FROM HTML::TokeParser</title>
191
Uses a true XML parser rather than a modified HTML parser.
194
Text and comment tokens include extracted text as well as literal text.
197
PI tokens include target and data as well as literal text.
200
No tokens for declarations.
203
No "textify" hash.
207
<title>EXAMPLES</title>
209
<title>Print method signatures from the XML version of this PODpage</title>
215
my $p=XML::TokeParser->new('tokeparser.xml',Noempty=>1) or die $!;
216
while ($p->get_tag('title') && $p->get_text('/title') ne 'METHODS') {
220
while (($t=$p->get_tag()->[0]) ne '/list') {
222
$p->get_tag('itemtext');
223
print $p->get_text('/itemtext'),"\n";
224
$p->get_tag('/item');
227
$p->get_tag('/list'); # assumes no nesting here!
234
<title>AUTHOR</title>
236
Eric Bohlman (ebohlman@omsdev.com)
239
Copyright (c) 2001 Eric Bohlman. All rights reserved. This program
240
is free software; you can redistribute it and/or modify it under the same
241
terms as Perl itself.
245
<title>SEE ALSO</title>