File: TokeParser.xml

package info (click to toggle)
libxml-tokeparser-perl 0.05-3.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 128 kB
  • sloc: perl: 279; xml: 252; makefile: 15
file content (252 lines) | stat: -rwxr-xr-x 7,875 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?xml version='1.0' encoding='iso-8859-1'?>
<pod xmlns="http://axkit.org/ns/2000/pod2xml">
<head>
	<title>XML::TokeParser - Simplified interface to XML::Parser</title>
</head>
<sect1>
<title>SYNOPSIS</title>
<verbatim><![CDATA[
use XML::TokeParser;
]]></verbatim>
<verbatim><![CDATA[
#parse from file
my $p=XML::TokeParser->new('file.xml')
]]></verbatim>
<verbatim><![CDATA[
#parse from open handle
open IN,'file.xml' or die $!;
my $p=XML::TokeParser->new(\*IN,Noempty=>1);
]]></verbatim>
<verbatim><![CDATA[
#parse literal text
my $text='<tag xmlns="http://www.omsdev.com">text</tag>';
my $p=XML::TokeParser->new(\$text,Namespaces=>1);
]]></verbatim>
<verbatim><![CDATA[
#read next token
my $token=$p->get_token();
]]></verbatim>
<verbatim><![CDATA[
#skip to <title> and read text
$p->get_tag('title');
$p->get_text();
]]></verbatim>
<verbatim><![CDATA[
#read text of next <para>, ignoring any internal markup
$p->get_tag('para');
$p->get_trimmed_text('/para');
]]></verbatim>
</sect1>
<sect1>
<title>DESCRIPTION</title>
<para>
XML::TokeParser provides a procedural (&quot;pull mode&quot;) 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
&quot;tokens,&quot; each corresponding to an XML::Parser event.
</para>
<para>
A token is a 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:
</para>
<list>
<item><itemtext>Start tag</itemtext>
<para>
The token has five elements: 'S', the element's name, a reference to a hash 
of attribute values keyed by attribute names, a reference to an array of 
attribute names in the order in which they appeared in the tag, and the 
literal text.
</para>
</item>
<item><itemtext>End tag</itemtext>
<para>
The token has three elements: 'E', the element's name, and the literal text.
</para>
</item>
<item><itemtext>Character data (text)</itemtext>
<para>
The token has three elements: 'T', the parsed text, and the literal text.  
All contiguous runs of text are gathered into single tokens; there will 
never be two 'T' tokens in a row.
</para>
</item>
<item><itemtext>Comment</itemtext>
<para>
The token has three elements: 'C', the parsed text of the comment, and the 
literal text.
</para>
</item>
<item><itemtext>Processing instruction</itemtext>
<para>
The token has four elements: 'PI', the target, the data, and the literal 
text.
</para>
</item>
</list>
<para>
The literal text includes any markup delimiters (pointy brackets, 
&lt;![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.
</para>
<para>
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.
</para>
</sect1>
<sect1>
<title>METHODS</title>
<list>
<item><itemtext>$p = XML::TokeParser-&gt;new($input, [options])</itemtext>
<para>
Creates a new parser, specifying the input source and any options.  If 
$input is a string, it is the name of the file to parse.  If $input is a 
reference to a string, that string is the actual text to parse.  If $input 
is a reference to a typeglob or an IO::Handle object corresponding to an 
open file or socket, the text read from the handle will be parsed.
</para>
<para>
Options are name=&gt;value pairs and can be any of the following:
</para>
</item>
<list>
<item><itemtext>Namespaces</itemtext>
<para>
If set to a true value, namespace processing is enabled.
</para>
</item>
<item><itemtext>ParseParamEnt</itemtext>
<para>
This option is passed on to the underlying XML::Parser object; see that 
module's documentation for details.
</para>
</item>
<item><itemtext>Noempty</itemtext>
<para>
If set to a true value, text tokens consisting of only whitespace (such as 
those created by indentation and line breaks in between tags) will be 
ignored.
</para>
</item>
<item><itemtext>Latin</itemtext>
<para>
If set to a true value, all text other than the literal text elements of 
tokens will be translated into the ISO 8859-1 (Latin-1) character encoding 
rather than the normal UTF-8 encoding.
</para>
</item>
<item><itemtext>Catalog</itemtext>
<para>
The value is the URI of a catalog file used to resolve PUBLIC and SYSTEM 
identifiers.  See XML::Catalog for details.
</para>
</item>
</list>
<item><itemtext>$token = $p-&gt;get_token()</itemtext>
<para>
Returns the next token, as an array reference, from the input.  Returns 
undef if there are no remaining tokens.
</para>
</item>
<item><itemtext>$p-&gt;unget_token($token,...)</itemtext>
<para>
Pushes tokens back so they will be re-read.  Useful if you've read one or 
more tokens to far.
</para>
</item>
<item><itemtext>$token = $p-&gt;get_tag( [$token] )</itemtext>
<para>
If no argument given, skips tokens until the next start tag or end tag 
token. If an argument is given, skips tokens until the start tag or end tag 
(if the argument begins with '/') for the named element.  The returned 
token does not include an event type code; its first element is the element 
name, prefixed by a '/' if the token is for an end tag.
</para>
</item>
<item><itemtext>$text = $p-&gt;get_text( [$token] )</itemtext>
<para>
If no argument given, returns the text at the current position, or an empty 
string if the next token is not a 'T' token.  If an argument is given, 
gathers up all text between the current position and the specified start or 
end tag, stripping out any intervening tags (much like the way a typical 
Web browser deals with unknown tags).
</para>
</item>
<item><itemtext>$text = $p-&gt;get_trimmed_text( [$token])</itemtext>
<para>
Like get_text(), but deletes any leading or trailing whitespaces and 
collapses multiple whitespace (including newlines) into single spaces.
</para>
</item>
</list>
</sect1>
<sect1>
<title>DIFFERENCES FROM HTML::TokeParser</title>
<para>
Uses a true XML parser rather than a modified HTML parser.
</para>
<para>
Text and comment tokens include extracted text as well as literal text.
</para>
<para>
PI tokens include target and data as well as literal text.
</para>
<para>
No tokens for declarations.
</para>
<para>
No &quot;textify&quot; hash.
</para>
</sect1>
<sect1>
<title>EXAMPLES</title>
<sect2>
<title>Print method signatures from the XML version of this PODpage</title>
<verbatim><![CDATA[
#!/usr/bin/perl -w
use strict;
use XML::TokeParser;
my $t;
my $p=XML::TokeParser->new('tokeparser.xml',Noempty=>1) or die $!;
while ($p->get_tag('title') && $p->get_text('/title') ne 'METHODS') {
  ;
}
$p->get_tag('list');
while (($t=$p->get_tag()->[0]) ne '/list') {
  if ($t eq 'item') {
    $p->get_tag('itemtext');
    print $p->get_text('/itemtext'),"\n";
    $p->get_tag('/item');
  }
  else {
    $p->get_tag('/list');  # assumes no nesting here!
  }
}
]]></verbatim>
</sect2>
</sect1>
<sect1>
<title>AUTHOR</title>
<para>
Eric Bohlman (ebohlman@omsdev.com)
</para>
<para>
Copyright (c) 2001 Eric Bohlman. All rights reserved. This program
is free software; you can redistribute it and/or modify it under the same
terms as Perl itself.
</para>
</sect1>
<sect1>
<title>SEE ALSO</title>
<verbatim><![CDATA[
XML::Parser
XML::Catalog
HTML::TokeParser
]]></verbatim>
</sect1>
</pod>