| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="dtd-handler-objects.html" /> |
| 13 | <link rel="prev" href="module-xml.sax.handler.html" /> |
| 14 | <link rel="parent" href="module-xml.sax.handler.html" /> |
| 15 | <link rel="next" href="dtd-handler-objects.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>13.10.1 ContentHandler Objects </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="13.10 xml.sax.handler " |
| 25 | href="module-xml.sax.handler.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="13.10 xml.sax.handler " |
| 28 | href="module-xml.sax.handler.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="13.10.2 DTDHandler Objects" |
| 31 | href="dtd-handler-objects.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-xml.sax.handler.html">13.10 xml.sax.handler </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-xml.sax.handler.html">13.10 xml.sax.handler </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="dtd-handler-objects.html">13.10.2 DTDHandler Objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION00151010000000000000000"></A><A NAME="content-handler-objects"></A> |
| 56 | <BR> |
| 57 | 13.10.1 ContentHandler Objects |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | Users are expected to subclass <tt class="class">ContentHandler</tt> to support their |
| 62 | application. The following methods are called by the parser on the |
| 63 | appropriate events in the input document: |
| 64 | |
| 65 | <P> |
| 66 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 67 | <td><nobr><b><tt id='l2h-4489' xml:id='l2h-4489' class="method">setDocumentLocator</tt></b>(</nobr></td> |
| 68 | <td><var>locator</var>)</td></tr></table></dt> |
| 69 | <dd> |
| 70 | Called by the parser to give the application a locator for locating |
| 71 | the origin of document events. |
| 72 | |
| 73 | <P> |
| 74 | SAX parsers are strongly encouraged (though not absolutely required) |
| 75 | to supply a locator: if it does so, it must supply the locator to |
| 76 | the application by invoking this method before invoking any of the |
| 77 | other methods in the DocumentHandler interface. |
| 78 | |
| 79 | <P> |
| 80 | The locator allows the application to determine the end position of |
| 81 | any document-related event, even if the parser is not reporting an |
| 82 | error. Typically, the application will use this information for |
| 83 | reporting its own errors (such as character content that does not |
| 84 | match an application's business rules). The information returned by |
| 85 | the locator is probably not sufficient for use with a search engine. |
| 86 | |
| 87 | <P> |
| 88 | Note that the locator will return correct information only during |
| 89 | the invocation of the events in this interface. The application |
| 90 | should not attempt to use it at any other time. |
| 91 | </dl> |
| 92 | |
| 93 | <P> |
| 94 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 95 | <td><nobr><b><tt id='l2h-4490' xml:id='l2h-4490' class="method">startDocument</tt></b>(</nobr></td> |
| 96 | <td><var></var>)</td></tr></table></dt> |
| 97 | <dd> |
| 98 | Receive notification of the beginning of a document. |
| 99 | |
| 100 | <P> |
| 101 | The SAX parser will invoke this method only once, before any other |
| 102 | methods in this interface or in DTDHandler (except for |
| 103 | <tt class="method">setDocumentLocator()</tt>). |
| 104 | </dl> |
| 105 | |
| 106 | <P> |
| 107 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 108 | <td><nobr><b><tt id='l2h-4491' xml:id='l2h-4491' class="method">endDocument</tt></b>(</nobr></td> |
| 109 | <td><var></var>)</td></tr></table></dt> |
| 110 | <dd> |
| 111 | Receive notification of the end of a document. |
| 112 | |
| 113 | <P> |
| 114 | The SAX parser will invoke this method only once, and it will be the |
| 115 | last method invoked during the parse. The parser shall not invoke |
| 116 | this method until it has either abandoned parsing (because of an |
| 117 | unrecoverable error) or reached the end of input. |
| 118 | </dl> |
| 119 | |
| 120 | <P> |
| 121 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 122 | <td><nobr><b><tt id='l2h-4492' xml:id='l2h-4492' class="method">startPrefixMapping</tt></b>(</nobr></td> |
| 123 | <td><var>prefix, uri</var>)</td></tr></table></dt> |
| 124 | <dd> |
| 125 | Begin the scope of a prefix-URI Namespace mapping. |
| 126 | |
| 127 | <P> |
| 128 | The information from this event is not necessary for normal |
| 129 | Namespace processing: the SAX XML reader will automatically replace |
| 130 | prefixes for element and attribute names when the |
| 131 | <code>feature_namespaces</code> feature is enabled (the default). |
| 132 | |
| 133 | <P> |
| 134 | There are cases, however, when applications need to use prefixes in |
| 135 | character data or in attribute values, where they cannot safely be |
| 136 | expanded automatically; the <tt class="method">startPrefixMapping()</tt> and |
| 137 | <tt class="method">endPrefixMapping()</tt> events supply the information to the |
| 138 | application to expand prefixes in those contexts itself, if |
| 139 | necessary. |
| 140 | |
| 141 | <P> |
| 142 | Note that <tt class="method">startPrefixMapping()</tt> and |
| 143 | <tt class="method">endPrefixMapping()</tt> events are not guaranteed to be properly |
| 144 | nested relative to each-other: all <tt class="method">startPrefixMapping()</tt> |
| 145 | events will occur before the corresponding <tt class="method">startElement()</tt> |
| 146 | event, and all <tt class="method">endPrefixMapping()</tt> events will occur after |
| 147 | the corresponding <tt class="method">endElement()</tt> event, but their order is |
| 148 | not guaranteed. |
| 149 | </dl> |
| 150 | |
| 151 | <P> |
| 152 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 153 | <td><nobr><b><tt id='l2h-4493' xml:id='l2h-4493' class="method">endPrefixMapping</tt></b>(</nobr></td> |
| 154 | <td><var>prefix</var>)</td></tr></table></dt> |
| 155 | <dd> |
| 156 | End the scope of a prefix-URI mapping. |
| 157 | |
| 158 | <P> |
| 159 | See <tt class="method">startPrefixMapping()</tt> for details. This event will |
| 160 | always occur after the corresponding <tt class="method">endElement()</tt> event, |
| 161 | but the order of <tt class="method">endPrefixMapping()</tt> events is not otherwise |
| 162 | guaranteed. |
| 163 | </dl> |
| 164 | |
| 165 | <P> |
| 166 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 167 | <td><nobr><b><tt id='l2h-4494' xml:id='l2h-4494' class="method">startElement</tt></b>(</nobr></td> |
| 168 | <td><var>name, attrs</var>)</td></tr></table></dt> |
| 169 | <dd> |
| 170 | Signals the start of an element in non-namespace mode. |
| 171 | |
| 172 | <P> |
| 173 | The <var>name</var> parameter contains the raw XML 1.0 name of the |
| 174 | element type as a string and the <var>attrs</var> parameter holds an |
| 175 | object of the <a class="ulink" href="attributes-objects.html" |
| 176 | ><tt class="class">Attributes</tt> |
| 177 | interface</a> containing the attributes of the |
| 178 | element. The object passed as <var>attrs</var> may be re-used by the |
| 179 | parser; holding on to a reference to it is not a reliable way to |
| 180 | keep a copy of the attributes. To keep a copy of the attributes, |
| 181 | use the <tt class="method">copy()</tt> method of the <var>attrs</var> object. |
| 182 | </dl> |
| 183 | |
| 184 | <P> |
| 185 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 186 | <td><nobr><b><tt id='l2h-4495' xml:id='l2h-4495' class="method">endElement</tt></b>(</nobr></td> |
| 187 | <td><var>name</var>)</td></tr></table></dt> |
| 188 | <dd> |
| 189 | Signals the end of an element in non-namespace mode. |
| 190 | |
| 191 | <P> |
| 192 | The <var>name</var> parameter contains the name of the element type, just |
| 193 | as with the <tt class="method">startElement()</tt> event. |
| 194 | </dl> |
| 195 | |
| 196 | <P> |
| 197 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 198 | <td><nobr><b><tt id='l2h-4496' xml:id='l2h-4496' class="method">startElementNS</tt></b>(</nobr></td> |
| 199 | <td><var>name, qname, attrs</var>)</td></tr></table></dt> |
| 200 | <dd> |
| 201 | Signals the start of an element in namespace mode. |
| 202 | |
| 203 | <P> |
| 204 | The <var>name</var> parameter contains the name of the element type as a |
| 205 | <code>(<var>uri</var>, <var>localname</var>)</code> tuple, the <var>qname</var> parameter |
| 206 | contains the raw XML 1.0 name used in the source document, and the |
| 207 | <var>attrs</var> parameter holds an instance of the |
| 208 | <a class="ulink" href="attributes-ns-objects.html" |
| 209 | ><tt class="class">AttributesNS</tt> interface</a> |
| 210 | containing the attributes of the element. If no namespace is |
| 211 | associated with the element, the <var>uri</var> component of <var>name</var> |
| 212 | will be <code>None</code>. The object passed as <var>attrs</var> may be |
| 213 | re-used by the parser; holding on to a reference to it is not a |
| 214 | reliable way to keep a copy of the attributes. To keep a copy of |
| 215 | the attributes, use the <tt class="method">copy()</tt> method of the <var>attrs</var> |
| 216 | object. |
| 217 | |
| 218 | <P> |
| 219 | Parsers may set the <var>qname</var> parameter to <code>None</code>, unless the |
| 220 | <code>feature_namespace_prefixes</code> feature is activated. |
| 221 | </dl> |
| 222 | |
| 223 | <P> |
| 224 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 225 | <td><nobr><b><tt id='l2h-4497' xml:id='l2h-4497' class="method">endElementNS</tt></b>(</nobr></td> |
| 226 | <td><var>name, qname</var>)</td></tr></table></dt> |
| 227 | <dd> |
| 228 | Signals the end of an element in namespace mode. |
| 229 | |
| 230 | <P> |
| 231 | The <var>name</var> parameter contains the name of the element type, just |
| 232 | as with the <tt class="method">startElementNS()</tt> method, likewise the |
| 233 | <var>qname</var> parameter. |
| 234 | </dl> |
| 235 | |
| 236 | <P> |
| 237 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 238 | <td><nobr><b><tt id='l2h-4498' xml:id='l2h-4498' class="method">characters</tt></b>(</nobr></td> |
| 239 | <td><var>content</var>)</td></tr></table></dt> |
| 240 | <dd> |
| 241 | Receive notification of character data. |
| 242 | |
| 243 | <P> |
| 244 | The Parser will call this method to report each chunk of character |
| 245 | data. SAX parsers may return all contiguous character data in a |
| 246 | single chunk, or they may split it into several chunks; however, all |
| 247 | of the characters in any single event must come from the same |
| 248 | external entity so that the Locator provides useful information. |
| 249 | |
| 250 | <P> |
| 251 | <var>content</var> may be a Unicode string or a byte string; the |
| 252 | <code>expat</code> reader module produces always Unicode strings. |
| 253 | |
| 254 | <P> |
| 255 | <span class="note"><b class="label">Note:</b> |
| 256 | The earlier SAX 1 interface provided by the Python |
| 257 | XML Special Interest Group used a more Java-like interface for this |
| 258 | method. Since most parsers used from Python did not take advantage |
| 259 | of the older interface, the simpler signature was chosen to replace |
| 260 | it. To convert old code to the new interface, use <var>content</var> |
| 261 | instead of slicing content with the old <var>offset</var> and |
| 262 | <var>length</var> parameters.</span> |
| 263 | </dl> |
| 264 | |
| 265 | <P> |
| 266 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 267 | <td><nobr><b><tt id='l2h-4499' xml:id='l2h-4499' class="method">ignorableWhitespace</tt></b>(</nobr></td> |
| 268 | <td><var>whitespace</var>)</td></tr></table></dt> |
| 269 | <dd> |
| 270 | Receive notification of ignorable whitespace in element content. |
| 271 | |
| 272 | <P> |
| 273 | Validating Parsers must use this method to report each chunk |
| 274 | of ignorable whitespace (see the W3C XML 1.0 recommendation, |
| 275 | section 2.10): non-validating parsers may also use this method |
| 276 | if they are capable of parsing and using content models. |
| 277 | |
| 278 | <P> |
| 279 | SAX parsers may return all contiguous whitespace in a single |
| 280 | chunk, or they may split it into several chunks; however, all |
| 281 | of the characters in any single event must come from the same |
| 282 | external entity, so that the Locator provides useful |
| 283 | information. |
| 284 | </dl> |
| 285 | |
| 286 | <P> |
| 287 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 288 | <td><nobr><b><tt id='l2h-4500' xml:id='l2h-4500' class="method">processingInstruction</tt></b>(</nobr></td> |
| 289 | <td><var>target, data</var>)</td></tr></table></dt> |
| 290 | <dd> |
| 291 | Receive notification of a processing instruction. |
| 292 | |
| 293 | <P> |
| 294 | The Parser will invoke this method once for each processing |
| 295 | instruction found: note that processing instructions may occur |
| 296 | before or after the main document element. |
| 297 | |
| 298 | <P> |
| 299 | A SAX parser should never report an XML declaration (XML 1.0, |
| 300 | section 2.8) or a text declaration (XML 1.0, section 4.3.1) using |
| 301 | this method. |
| 302 | </dl> |
| 303 | |
| 304 | <P> |
| 305 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 306 | <td><nobr><b><tt id='l2h-4501' xml:id='l2h-4501' class="method">skippedEntity</tt></b>(</nobr></td> |
| 307 | <td><var>name</var>)</td></tr></table></dt> |
| 308 | <dd> |
| 309 | Receive notification of a skipped entity. |
| 310 | |
| 311 | <P> |
| 312 | The Parser will invoke this method once for each entity |
| 313 | skipped. Non-validating processors may skip entities if they have |
| 314 | not seen the declarations (because, for example, the entity was |
| 315 | declared in an external DTD subset). All processors may skip |
| 316 | external entities, depending on the values of the |
| 317 | <code>feature_external_ges</code> and the |
| 318 | <code>feature_external_pes</code> properties. |
| 319 | </dl> |
| 320 | |
| 321 | <P> |
| 322 | |
| 323 | <DIV CLASS="navigation"> |
| 324 | <div class='online-navigation'> |
| 325 | <p></p><hr /> |
| 326 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 327 | <tr> |
| 328 | <td class='online-navigation'><a rel="prev" title="13.10 xml.sax.handler " |
| 329 | href="module-xml.sax.handler.html"><img src='../icons/previous.png' |
| 330 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 331 | <td class='online-navigation'><a rel="parent" title="13.10 xml.sax.handler " |
| 332 | href="module-xml.sax.handler.html"><img src='../icons/up.png' |
| 333 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 334 | <td class='online-navigation'><a rel="next" title="13.10.2 DTDHandler Objects" |
| 335 | href="dtd-handler-objects.html"><img src='../icons/next.png' |
| 336 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 337 | <td align="center" width="100%">Python Library Reference</td> |
| 338 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 339 | href="contents.html"><img src='../icons/contents.png' |
| 340 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 341 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 342 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 343 | <td class='online-navigation'><a rel="index" title="Index" |
| 344 | href="genindex.html"><img src='../icons/index.png' |
| 345 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 346 | </tr></table> |
| 347 | <div class='online-navigation'> |
| 348 | <b class="navlabel">Previous:</b> |
| 349 | <a class="sectref" rel="prev" href="module-xml.sax.handler.html">13.10 xml.sax.handler </A> |
| 350 | <b class="navlabel">Up:</b> |
| 351 | <a class="sectref" rel="parent" href="module-xml.sax.handler.html">13.10 xml.sax.handler </A> |
| 352 | <b class="navlabel">Next:</b> |
| 353 | <a class="sectref" rel="next" href="dtd-handler-objects.html">13.10.2 DTDHandler Objects</A> |
| 354 | </div> |
| 355 | </div> |
| 356 | <hr /> |
| 357 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 358 | </DIV> |
| 359 | <!--End of Navigation Panel--> |
| 360 | <ADDRESS> |
| 361 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 362 | </ADDRESS> |
| 363 | </BODY> |
| 364 | </HTML> |