Debian Codegraph @

doc/sax-2.0.html

raw symbols SCIP
1 <!-- $Id: sax-2.0.html,v 1.6 2002/01/21 19:21:43 darobin Exp $ -->
2 <html>
3 <head>
4 <title>Perl SAX 2.0 Binding</title>
5 </head>
6 <body>
7
8 <h1>Perl SAX 2.0 Binding</h1>
9
10 <p>SAX (Simple API for XML) is a common parser interface for XML
11 parsers. It allows application writers to write applications that use
12 XML parsers, but are independent of which parser is actually used.</p>
13
14 <p>This document describes the version of SAX used by Perl modules.
15 The original version of SAX 2.0, for Java, is described at <a
16 href="http://sax.sourceforge.net/">http://sax.sourceforge.net/</a>.</p>
17
18 <p>There are two basic interfaces in the Perl version of SAX, the
19 parser interface and the handler interface. The parser interface
20 creates new parser instances, starts parsing, and provides additional
21 information to handlers on request. The handler interface is used to
22 receive parse events from the parser. This pattern is also commonly
23 called "Producer and Consumer" or "Generator and Sink". Note that the
24 parser doesn't have to be an XML parser, all it needs to do is provide
25 a stream of events to the handler as if it were parsing XML. But the
26 actual data from which the events are generated can be anything, a Perl
27 object, a CSV file, a database table...
28 </p>
29
30 <p>SAX is typically used like this:
31
32 <pre>
33 my $handler = MyHandler->new();
34 my $parser = AnySAXParser->new( Handler => $handler );
35 $parser->parse($uri);
36 </pre></p>
37
38 <p>Handlers are typically written like this:
39
40 <pre>
41 package MyHandler;
42
43 sub new {
44 my $type = shift;
45 return bless {}, $type;
46 }
47
48 sub start_element {
49 my ($self, $element) = @_;
50
51 print "Starting element $element->{Name}\n";
52 }
53
54 sub end_element {
55 my ($self, $element) = @_;
56
57 print "Ending element $element->{Name}\n";
58 }
59
60 sub characters {
61 my ($self, $characters) = @_;
62
63 print "characters: $characters->{Data}\n";
64 }
65
66 1;
67 </pre></p>
68
69 <h2>Basic SAX Parser</h2>
70
71 <p>These methods and options are the most commonly used with SAX
72 parsers and event generators.</p>
73
74 <p>Applications may not invoke a <tt>parse()</tt> method again while a
75 parse is in progress (they should create a new SAX parser instead for
76 each nested XML document). Once a parse is complete, an application
77 may reuse the same parser object, possibly with a different input
78 source.</p>
79
80 <p>During the parse, the parser will provide information about the XML
81 document through the registered event handlers. Note that an event that
82 hasn't been registered (ie that doesn't have its corresponding method in
83 the handler's class) will <b>not</b> be called. This allows one to only
84 get the events one is interested in.
85 </p>
86
87 <p>
88 <dl><dt><b><tt class='function'>parse</tt></b>(<var>uri</var> [, <var>options</var>])</dt>
89 <dd>
90 Parses the XML instance identified by <var>uri</var> (a system
91 identifier). <var>options</var> can be a list of option, value pairs
92 or a hash. Options include <tt>Handler</tt>, features and properties,
93 and advanced SAX parser options. <tt>parse()</tt> returns the result
94 of calling the <tt>end_document()</tt> handler. The options supported
95 by <tt>parse()</tt> may vary slightly if what is being "parsed" isn't
96 XML.
97 </dd></dl></p>
98
99 <p>
100 <dl><dt><b><tt class='function'>parse_file</tt></b>(<var>stream</var> [, <var>options</var>])</dt>
101 <dd>
102 Parses the XML instance in the already opened <var>stream</var>, an
103 IO::Handler or similar. <var>options</var> are the same as for <tt
104 class='function'>parse()</tt>. <tt>parse_file()</tt> returns the result
105 of calling the <tt>end_document()</tt> handler.</dd></dl></p>
106
107 <p>
108 <dl><dt><b><tt class='function'>parse_string</tt></b>(<var>string</var> [, <var>options</var>])</dt>
109 <dd>
110 Parses the XML instance in <var>string</var>. <var>options</var> are
111 the same as for <tt class='function'>parse()</tt>.
112 <tt>parse_string()</tt> returns the result of calling the
113 <tt>end_document()</tt> handler.</dd></dl></p>
114
115 <p>
116 <dl><dt><b><tt>Handler</tt></b></dt>
117 <dd>
118 The default handler object to receive all events from the parser.
119 Applications may change <tt>Handler</tt> in the middle of the parse
120 and the SAX parser will begin using the new handler
121 immediately. The <a href="sax-2.0-adv.html">Advanced SAX</a> document
122 lists a number of more specialized handlers that can be used should you
123 wish to dispatch different types of events to different objects.
124 </dd></dl></p>
125
126 <h2><a name="BasicHandler">Basic SAX Handler</a></h2>
127
128 <p>These methods are the most commonly used by SAX handlers.</p>
129
130 <p>
131 <dl><dt><b><tt class='function'>start_document</tt></b>(<var>document</var>)</dt>
132 <dd>
133 Receive notification of the beginning of a document.
134
135 <p>The SAX parser will invoke this method only once, before any other
136 methods (except for <tt>set_document_locator()</tt> in advanced SAX
137 handlers).</p>
138
139 No properties are defined for this event (<var>document</var> is
140 empty).</dd></dl></p>
141
142 <p>
143 <dl><dt><b><tt class='function'>end_document</tt></b>(<var>document</var>)</dt>
144 <dd>
145 Receive notification of the end of a document.
146
147 <p>The SAX parser will invoke this method only once, and it will be
148 the last method invoked during the parse. The parser shall not invoke
149 this method until it has either abandoned parsing (because of an
150 unrecoverable error) or reached the end of input.</p>
151
152 <p>No properties are defined for this event (<var>document</var> is
153 empty).</p>
154
155 The return value of <tt>end_document()</tt> is returned by the
156 parser's <tt>parse()</tt> methods.</dd></dl></p>
157
158 <p>
159 <dl><dt><b><tt class='function'>start_element</tt></b>(<var>element</var>)</dt>
160 <dd>
161 Receive notification of the start of an element.
162
163 <p>The Parser will invoke this method at the beginning of every
164 element in the XML document; there will be a corresponding
165 <tt>end_element()</tt> event for every <tt>start_element()</tt> event (even when the
166 element is empty). All of the element's content will be reported, in
167 order, before the corresponding <tt>end_element()</tt> event.</p>
168
169 <var>element</var> is a hash with these properties:
170
171 <blockquote>
172 <table>
173 <tr><td><b><tt>Name</tt></b></td>
174 <td>The element type name (including prefix).</td></tr>
175 <tr><td><b><tt>Attributes</tt></b></td>
176 <td>The attributes attached to the element, if any.</td></tr>
177 </table>
178 </blockquote>
179
180 If namespace processing is turned on (which is the default), these
181 properties are also available:
182
183 <blockquote>
184 <table>
185 <tr><td><b><tt>NamespaceURI</tt></b></td>
186 <td>The namespace of this element.</td></tr>
187 <tr><td><b><tt>Prefix</tt></b></td>
188 <td>The namespace prefix used on this element.</td></tr>
189 <tr><td><b><tt>LocalName</tt></b></td>
190 <td>The local name of this element.</td></tr>
191 </table>
192 </blockquote>
193
194 <tt>Attributes</tt> is a hash keyed by JClark namespace notation. That
195 is, the keys are of the form "{NamespaceURI}LocalName". If the attribute
196 has no NamespaceURI, then it is simply "{}LocalName". Each attribute is
197 a hash with these properties:
198
199 <blockquote>
200 <table>
201 <tr><td><b><tt>Name</tt></b></td>
202 <td>The attribute name (including prefix).</td></tr>
203 <tr><td><b><tt>Value</tt></b></td>
204 <td>The normalized value of the attribute.</td></tr>
205 <tr><td><b><tt>NamespaceURI</tt></b></td>
206 <td>The namespace of this attribute.</td></tr>
207 <tr><td><b><tt>Prefix</tt></b></td>
208 <td>The namespace prefix used on this attribute.</td></tr>
209 <tr><td><b><tt>LocalName</tt></b></td>
210 <td>The local name of this attribute.</td></tr>
211 </table>
212 </blockquote>
213 </dd>
214 </dl>
215 </p>
216
217 <p>
218 <dl><dt><b><tt class='function'>end_element</tt></b>(<var>element</var>)</dt>
219 <dd>
220 Receive notification of the end of an element.
221
222 <p>The SAX parser will invoke this method at the end of every element
223 in the XML document; there will be a corresponding <tt
224 class='function'>start_element()</tt> event for every <tt
225 class='function'>end_element()</tt> event (even when the element is
226 empty).</p>
227
228 <var>element</var> is a hash with these properties:
229
230 <blockquote>
231 <table>
232 <tr><td><b><tt>Name</tt></b></td>
233 <td>The element type name (including prefix).</td></tr>
234 </table>
235 </blockquote>
236
237 If namespace processing is turned on (which is the default), these
238 properties are also available:
239
240 <blockquote>
241 <table>
242 <tr><td><b><tt>NamespaceURI</tt></b></td>
243 <td>The namespace of this element.</td></tr>
244 <tr><td><b><tt>Prefix</tt></b></td>
245 <td>The namespace prefix used on this element.</td></tr>
246 <tr><td><b><tt>LocalName</tt></b></td>
247 <td>The local name of this element.</td></tr>
248 </table>
249 </blockquote></dd>
250 </dl></p>
251
252 <p>
253 <dl><dt><b><tt class='function'>characters</tt></b>(<var>characters</var>)</dt>
254 <dd>
255 Receive notification of character data.
256
257 <p>The Parser will call this method to report each chunk of character
258 data. SAX parsers may return all contiguous character data in a
259 single chunk, or they may split it into several chunks (however, all
260 of the characters in any single event must come from the same external
261 entity so that the Locator provides useful information).</p>
262
263 <p><var>characters</var> is a hash with this property:</p>
264
265 <blockquote>
266 <table>
267 <tr><td><b><tt>Data</tt></b></td>
268 <td>The characters from the XML document.</td></tr>
269 </table>
270 </blockquote></dd>
271 </dl></p>
272
273 <p>
274 <dl><dt><b><tt class='function'>ignorable_whitespace</tt></b>(<var>characters</var>)</dt>
275 <dd>
276 Receive notification of ignorable whitespace in element content.
277
278 <p>Validating Parsers must use this method to report each chunk of
279 ignorable whitespace (see the W3C XML 1.0 recommendation, section
280 2.10): non-validating parsers may also use this method if they are
281 capable of parsing and using content models.</p>
282
283 <p>SAX parsers may return all contiguous whitespace in a single chunk,
284 or they may split it into several chunks; however, all of the
285 characters in any single event must come from the same external
286 entity, so that the Locator provides useful information.</p>
287
288 <p><var>characters</var> is a hash with this property:</p>
289
290 <blockquote>
291 <table>
292 <tr><td><b><tt>Data</tt></b></td>
293 <td>The whitespace characters from the XML document.</td></tr>
294 </table>
295 </blockquote></dd>
296 </dl></p>
297
298 <h2><a name="Exceptions">Exceptions</a></h2>
299
300 <p>
301 Conformant XML parsers are required to abort processing when
302 well-formedness or validation errors occur. In Perl, SAX parsers use
303 <tt>die()</tt> to signal these errors. To catch these errors and prevent
304 them from killing your program, use <tt>eval{}</tt>:
305
306 <pre>
307 eval { $parser->parse($uri) };
308 if ($@) {
309 # handle error
310 }
311 </pre>
312 </p>
313
314 <p>
315 Exceptions can also be thrown when setting features or properties
316 on the SAX parser (see advanced SAX below).</p>
317
318 <p>
319 Exception values (<tt>$@</tt>) in SAX are hashes blessed into the
320 package that defines their type, and have the following properties:
321 </p>
322
323 <blockquote>
324 <table>
325 <tr><td><b><tt>Message</tt></b></td>
326 <td>A detail message for this exception.</td></tr>
327 <tr><td><b><tt>Exception</tt></b></td>
328 <td>The embedded exception, or <tt>undef</tt> if there is none.</td></tr>
329 </table>
330 </blockquote>
331
332 If the exception is raised due to parse errors, these
333 properties are also available:
334
335 <blockquote>
336 <table>
337 <tr><td><b><tt>ColumnNumber</tt></b></td>
338 <td>The column number of the end of the text where the exception
339 occurred.</td></tr>
340 <tr><td><b><tt>LineNumber</tt></b></td>
341 <td>The line number of the end of the text where the exception
342 occurred.</td></tr>
343 <tr><td><b><tt>PublicId</tt></b></td>
344 <td>The public identifier of the entity where the exception
345 occurred.</td></tr>
346 <tr><td><b><tt>SystemId</tt></b></td>
347 <td>The system identifier of the entity where the exception
348 occurred.</td></tr>
349 </table>
350 </blockquote>
351
352
353 <p></p><hr />
354 <h2>Advanced SAX</h2>
355 <ul>
356 <li><a href="sax-2.0-adv.html#Parsers">SAX Parsers</a></li>
357 <li><a href="sax-2.0-adv.html#Features">Features</a></li>
358 <li><a href="sax-2.0-adv.html#InputSources">Input Sources</a></li>
359 <li><a href="sax-2.0-adv.html#Handlers">SAX Handlers</a></li>
360 <li><a href="sax-2.0-adv.html#Filters">SAX Filters</a></li>
361 <li><a href="sax-2.0-adv.html#Java">Java and DOM Compatibility</a></li>
362 </ul>
363
364 </body>
365 </html>