/Doc/library/xml.sax.reader.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 386 lines · 207 code · 179 blank · 0 comment · 0 complexity · c925258f8d5ac3691b57594ab4c50899 MD5 · raw file

  1. :mod:`xml.sax.xmlreader` --- Interface for XML parsers
  2. ======================================================
  3. .. module:: xml.sax.xmlreader
  4. :synopsis: Interface which SAX-compliant XML parsers must implement.
  5. .. moduleauthor:: Lars Marius Garshol <larsga@garshol.priv.no>
  6. .. sectionauthor:: Martin v. Lรถwis <martin@v.loewis.de>
  7. .. versionadded:: 2.0
  8. SAX parsers implement the :class:`XMLReader` interface. They are implemented in
  9. a Python module, which must provide a function :func:`create_parser`. This
  10. function is invoked by :func:`xml.sax.make_parser` with no arguments to create
  11. a new parser object.
  12. .. class:: XMLReader()
  13. Base class which can be inherited by SAX parsers.
  14. .. class:: IncrementalParser()
  15. In some cases, it is desirable not to parse an input source at once, but to feed
  16. chunks of the document as they get available. Note that the reader will normally
  17. not read the entire file, but read it in chunks as well; still :meth:`parse`
  18. won't return until the entire document is processed. So these interfaces should
  19. be used if the blocking behaviour of :meth:`parse` is not desirable.
  20. When the parser is instantiated it is ready to begin accepting data from the
  21. feed method immediately. After parsing has been finished with a call to close
  22. the reset method must be called to make the parser ready to accept new data,
  23. either from feed or using the parse method.
  24. Note that these methods must *not* be called during parsing, that is, after
  25. parse has been called and before it returns.
  26. By default, the class also implements the parse method of the XMLReader
  27. interface using the feed, close and reset methods of the IncrementalParser
  28. interface as a convenience to SAX 2.0 driver writers.
  29. .. class:: Locator()
  30. Interface for associating a SAX event with a document location. A locator object
  31. will return valid results only during calls to DocumentHandler methods; at any
  32. other time, the results are unpredictable. If information is not available,
  33. methods may return ``None``.
  34. .. class:: InputSource([systemId])
  35. Encapsulation of the information needed by the :class:`XMLReader` to read
  36. entities.
  37. This class may include information about the public identifier, system
  38. identifier, byte stream (possibly with character encoding information) and/or
  39. the character stream of an entity.
  40. Applications will create objects of this class for use in the
  41. :meth:`XMLReader.parse` method and for returning from
  42. EntityResolver.resolveEntity.
  43. An :class:`InputSource` belongs to the application, the :class:`XMLReader` is
  44. not allowed to modify :class:`InputSource` objects passed to it from the
  45. application, although it may make copies and modify those.
  46. .. class:: AttributesImpl(attrs)
  47. This is an implementation of the :class:`Attributes` interface (see section
  48. :ref:`attributes-objects`). This is a dictionary-like object which
  49. represents the element attributes in a :meth:`startElement` call. In addition
  50. to the most useful dictionary operations, it supports a number of other
  51. methods as described by the interface. Objects of this class should be
  52. instantiated by readers; *attrs* must be a dictionary-like object containing
  53. a mapping from attribute names to attribute values.
  54. .. class:: AttributesNSImpl(attrs, qnames)
  55. Namespace-aware variant of :class:`AttributesImpl`, which will be passed to
  56. :meth:`startElementNS`. It is derived from :class:`AttributesImpl`, but
  57. understands attribute names as two-tuples of *namespaceURI* and
  58. *localname*. In addition, it provides a number of methods expecting qualified
  59. names as they appear in the original document. This class implements the
  60. :class:`AttributesNS` interface (see section :ref:`attributes-ns-objects`).
  61. .. _xmlreader-objects:
  62. XMLReader Objects
  63. -----------------
  64. The :class:`XMLReader` interface supports the following methods:
  65. .. method:: XMLReader.parse(source)
  66. Process an input source, producing SAX events. The *source* object can be a
  67. system identifier (a string identifying the input source -- typically a file
  68. name or an URL), a file-like object, or an :class:`InputSource` object. When
  69. :meth:`parse` returns, the input is completely processed, and the parser object
  70. can be discarded or reset. As a limitation, the current implementation only
  71. accepts byte streams; processing of character streams is for further study.
  72. .. method:: XMLReader.getContentHandler()
  73. Return the current :class:`ContentHandler`.
  74. .. method:: XMLReader.setContentHandler(handler)
  75. Set the current :class:`ContentHandler`. If no :class:`ContentHandler` is set,
  76. content events will be discarded.
  77. .. method:: XMLReader.getDTDHandler()
  78. Return the current :class:`DTDHandler`.
  79. .. method:: XMLReader.setDTDHandler(handler)
  80. Set the current :class:`DTDHandler`. If no :class:`DTDHandler` is set, DTD
  81. events will be discarded.
  82. .. method:: XMLReader.getEntityResolver()
  83. Return the current :class:`EntityResolver`.
  84. .. method:: XMLReader.setEntityResolver(handler)
  85. Set the current :class:`EntityResolver`. If no :class:`EntityResolver` is set,
  86. attempts to resolve an external entity will result in opening the system
  87. identifier for the entity, and fail if it is not available.
  88. .. method:: XMLReader.getErrorHandler()
  89. Return the current :class:`ErrorHandler`.
  90. .. method:: XMLReader.setErrorHandler(handler)
  91. Set the current error handler. If no :class:`ErrorHandler` is set, errors will
  92. be raised as exceptions, and warnings will be printed.
  93. .. method:: XMLReader.setLocale(locale)
  94. Allow an application to set the locale for errors and warnings.
  95. SAX parsers are not required to provide localization for errors and warnings; if
  96. they cannot support the requested locale, however, they must throw a SAX
  97. exception. Applications may request a locale change in the middle of a parse.
  98. .. method:: XMLReader.getFeature(featurename)
  99. Return the current setting for feature *featurename*. If the feature is not
  100. recognized, :exc:`SAXNotRecognizedException` is raised. The well-known
  101. featurenames are listed in the module :mod:`xml.sax.handler`.
  102. .. method:: XMLReader.setFeature(featurename, value)
  103. Set the *featurename* to *value*. If the feature is not recognized,
  104. :exc:`SAXNotRecognizedException` is raised. If the feature or its setting is not
  105. supported by the parser, *SAXNotSupportedException* is raised.
  106. .. method:: XMLReader.getProperty(propertyname)
  107. Return the current setting for property *propertyname*. If the property is not
  108. recognized, a :exc:`SAXNotRecognizedException` is raised. The well-known
  109. propertynames are listed in the module :mod:`xml.sax.handler`.
  110. .. method:: XMLReader.setProperty(propertyname, value)
  111. Set the *propertyname* to *value*. If the property is not recognized,
  112. :exc:`SAXNotRecognizedException` is raised. If the property or its setting is
  113. not supported by the parser, *SAXNotSupportedException* is raised.
  114. .. _incremental-parser-objects:
  115. IncrementalParser Objects
  116. -------------------------
  117. Instances of :class:`IncrementalParser` offer the following additional methods:
  118. .. method:: IncrementalParser.feed(data)
  119. Process a chunk of *data*.
  120. .. method:: IncrementalParser.close()
  121. Assume the end of the document. That will check well-formedness conditions that
  122. can be checked only at the end, invoke handlers, and may clean up resources
  123. allocated during parsing.
  124. .. method:: IncrementalParser.reset()
  125. This method is called after close has been called to reset the parser so that it
  126. is ready to parse new documents. The results of calling parse or feed after
  127. close without calling reset are undefined.
  128. .. _locator-objects:
  129. Locator Objects
  130. ---------------
  131. Instances of :class:`Locator` provide these methods:
  132. .. method:: Locator.getColumnNumber()
  133. Return the column number where the current event ends.
  134. .. method:: Locator.getLineNumber()
  135. Return the line number where the current event ends.
  136. .. method:: Locator.getPublicId()
  137. Return the public identifier for the current event.
  138. .. method:: Locator.getSystemId()
  139. Return the system identifier for the current event.
  140. .. _input-source-objects:
  141. InputSource Objects
  142. -------------------
  143. .. method:: InputSource.setPublicId(id)
  144. Sets the public identifier of this :class:`InputSource`.
  145. .. method:: InputSource.getPublicId()
  146. Returns the public identifier of this :class:`InputSource`.
  147. .. method:: InputSource.setSystemId(id)
  148. Sets the system identifier of this :class:`InputSource`.
  149. .. method:: InputSource.getSystemId()
  150. Returns the system identifier of this :class:`InputSource`.
  151. .. method:: InputSource.setEncoding(encoding)
  152. Sets the character encoding of this :class:`InputSource`.
  153. The encoding must be a string acceptable for an XML encoding declaration (see
  154. section 4.3.3 of the XML recommendation).
  155. The encoding attribute of the :class:`InputSource` is ignored if the
  156. :class:`InputSource` also contains a character stream.
  157. .. method:: InputSource.getEncoding()
  158. Get the character encoding of this InputSource.
  159. .. method:: InputSource.setByteStream(bytefile)
  160. Set the byte stream (a Python file-like object which does not perform
  161. byte-to-character conversion) for this input source.
  162. The SAX parser will ignore this if there is also a character stream specified,
  163. but it will use a byte stream in preference to opening a URI connection itself.
  164. If the application knows the character encoding of the byte stream, it should
  165. set it with the setEncoding method.
  166. .. method:: InputSource.getByteStream()
  167. Get the byte stream for this input source.
  168. The getEncoding method will return the character encoding for this byte stream,
  169. or None if unknown.
  170. .. method:: InputSource.setCharacterStream(charfile)
  171. Set the character stream for this input source. (The stream must be a Python 1.6
  172. Unicode-wrapped file-like that performs conversion to Unicode strings.)
  173. If there is a character stream specified, the SAX parser will ignore any byte
  174. stream and will not attempt to open a URI connection to the system identifier.
  175. .. method:: InputSource.getCharacterStream()
  176. Get the character stream for this input source.
  177. .. _attributes-objects:
  178. The :class:`Attributes` Interface
  179. ---------------------------------
  180. :class:`Attributes` objects implement a portion of the mapping protocol,
  181. including the methods :meth:`copy`, :meth:`get`, :meth:`has_key`, :meth:`items`,
  182. :meth:`keys`, and :meth:`values`. The following methods are also provided:
  183. .. method:: Attributes.getLength()
  184. Return the number of attributes.
  185. .. method:: Attributes.getNames()
  186. Return the names of the attributes.
  187. .. method:: Attributes.getType(name)
  188. Returns the type of the attribute *name*, which is normally ``'CDATA'``.
  189. .. method:: Attributes.getValue(name)
  190. Return the value of attribute *name*.
  191. .. getValueByQName, getNameByQName, getQNameByName, getQNames available
  192. .. here already, but documented only for derived class.
  193. .. _attributes-ns-objects:
  194. The :class:`AttributesNS` Interface
  195. -----------------------------------
  196. This interface is a subtype of the :class:`Attributes` interface (see section
  197. :ref:`attributes-objects`). All methods supported by that interface are also
  198. available on :class:`AttributesNS` objects.
  199. The following methods are also available:
  200. .. method:: AttributesNS.getValueByQName(name)
  201. Return the value for a qualified name.
  202. .. method:: AttributesNS.getNameByQName(name)
  203. Return the ``(namespace, localname)`` pair for a qualified *name*.
  204. .. method:: AttributesNS.getQNameByName(name)
  205. Return the qualified name for a ``(namespace, localname)`` pair.
  206. .. method:: AttributesNS.getQNames()
  207. Return the qualified names of all attributes.