Home | History | Annotate | Download | only in library
      1 :mod:`xml.sax.xmlreader` --- Interface for XML parsers
      2 ======================================================
      3 
      4 .. module:: xml.sax.xmlreader
      5    :synopsis: Interface which SAX-compliant XML parsers must implement.
      6 
      7 .. moduleauthor:: Lars Marius Garshol <larsga (a] garshol.priv.no>
      8 .. sectionauthor:: Martin v. Lwis <martin (a] v.loewis.de>
      9 
     10 **Source code:** :source:`Lib/xml/sax/xmlreader.py`
     11 
     12 --------------
     13 
     14 SAX parsers implement the :class:`XMLReader` interface. They are implemented in
     15 a Python module, which must provide a function :func:`create_parser`. This
     16 function is invoked by  :func:`xml.sax.make_parser` with no arguments to create
     17 a new  parser object.
     18 
     19 
     20 .. class:: XMLReader()
     21 
     22    Base class which can be inherited by SAX parsers.
     23 
     24 
     25 .. class:: IncrementalParser()
     26 
     27    In some cases, it is desirable not to parse an input source at once, but to feed
     28    chunks of the document as they get available. Note that the reader will normally
     29    not read the entire file, but read it in chunks as well; still :meth:`parse`
     30    won't return until the entire document is processed. So these interfaces should
     31    be used if the blocking behaviour of :meth:`parse` is not desirable.
     32 
     33    When the parser is instantiated it is ready to begin accepting data from the
     34    feed method immediately. After parsing has been finished with a call to close
     35    the reset method must be called to make the parser ready to accept new data,
     36    either from feed or using the parse method.
     37 
     38    Note that these methods must *not* be called during parsing, that is, after
     39    parse has been called and before it returns.
     40 
     41    By default, the class also implements the parse method of the XMLReader
     42    interface using the feed, close and reset methods of the IncrementalParser
     43    interface as a convenience to SAX 2.0 driver writers.
     44 
     45 
     46 .. class:: Locator()
     47 
     48    Interface for associating a SAX event with a document location. A locator object
     49    will return valid results only during calls to DocumentHandler methods; at any
     50    other time, the results are unpredictable. If information is not available,
     51    methods may return ``None``.
     52 
     53 
     54 .. class:: InputSource(system_id=None)
     55 
     56    Encapsulation of the information needed by the :class:`XMLReader` to read
     57    entities.
     58 
     59    This class may include information about the public identifier, system
     60    identifier, byte stream (possibly with character encoding information) and/or
     61    the character stream of an entity.
     62 
     63    Applications will create objects of this class for use in the
     64    :meth:`XMLReader.parse` method and for returning from
     65    EntityResolver.resolveEntity.
     66 
     67    An :class:`InputSource` belongs to the application, the :class:`XMLReader` is
     68    not allowed to modify :class:`InputSource` objects passed to it from the
     69    application, although it may make copies and modify those.
     70 
     71 
     72 .. class:: AttributesImpl(attrs)
     73 
     74    This is an implementation of the :class:`Attributes` interface (see section
     75    :ref:`attributes-objects`).  This is a dictionary-like object which
     76    represents the element attributes in a :meth:`startElement` call. In addition
     77    to the most useful dictionary operations, it supports a number of other
     78    methods as described by the interface. Objects of this class should be
     79    instantiated by readers; *attrs* must be a dictionary-like object containing
     80    a mapping from attribute names to attribute values.
     81 
     82 
     83 .. class:: AttributesNSImpl(attrs, qnames)
     84 
     85    Namespace-aware variant of :class:`AttributesImpl`, which will be passed to
     86    :meth:`startElementNS`. It is derived from :class:`AttributesImpl`, but
     87    understands attribute names as two-tuples of *namespaceURI* and
     88    *localname*. In addition, it provides a number of methods expecting qualified
     89    names as they appear in the original document.  This class implements the
     90    :class:`AttributesNS` interface (see section :ref:`attributes-ns-objects`).
     91 
     92 
     93 .. _xmlreader-objects:
     94 
     95 XMLReader Objects
     96 -----------------
     97 
     98 The :class:`XMLReader` interface supports the following methods:
     99 
    100 
    101 .. method:: XMLReader.parse(source)
    102 
    103    Process an input source, producing SAX events. The *source* object can be a
    104    system identifier (a string identifying the input source -- typically a file
    105    name or a URL), a file-like object, or an :class:`InputSource` object. When
    106    :meth:`parse` returns, the input is completely processed, and the parser object
    107    can be discarded or reset.
    108 
    109    .. versionchanged:: 3.5
    110       Added support of character streams.
    111 
    112 
    113 .. method:: XMLReader.getContentHandler()
    114 
    115    Return the current :class:`~xml.sax.handler.ContentHandler`.
    116 
    117 
    118 .. method:: XMLReader.setContentHandler(handler)
    119 
    120    Set the current :class:`~xml.sax.handler.ContentHandler`.  If no
    121    :class:`~xml.sax.handler.ContentHandler` is set, content events will be
    122    discarded.
    123 
    124 
    125 .. method:: XMLReader.getDTDHandler()
    126 
    127    Return the current :class:`~xml.sax.handler.DTDHandler`.
    128 
    129 
    130 .. method:: XMLReader.setDTDHandler(handler)
    131 
    132    Set the current :class:`~xml.sax.handler.DTDHandler`.  If no
    133    :class:`~xml.sax.handler.DTDHandler` is set, DTD
    134    events will be discarded.
    135 
    136 
    137 .. method:: XMLReader.getEntityResolver()
    138 
    139    Return the current :class:`~xml.sax.handler.EntityResolver`.
    140 
    141 
    142 .. method:: XMLReader.setEntityResolver(handler)
    143 
    144    Set the current :class:`~xml.sax.handler.EntityResolver`.  If no
    145    :class:`~xml.sax.handler.EntityResolver` is set,
    146    attempts to resolve an external entity will result in opening the system
    147    identifier for the entity, and fail if it is not available.
    148 
    149 
    150 .. method:: XMLReader.getErrorHandler()
    151 
    152    Return the current :class:`~xml.sax.handler.ErrorHandler`.
    153 
    154 
    155 .. method:: XMLReader.setErrorHandler(handler)
    156 
    157    Set the current error handler.  If no :class:`~xml.sax.handler.ErrorHandler`
    158    is set, errors will be raised as exceptions, and warnings will be printed.
    159 
    160 
    161 .. method:: XMLReader.setLocale(locale)
    162 
    163    Allow an application to set the locale for errors and warnings.
    164 
    165    SAX parsers are not required to provide localization for errors and warnings; if
    166    they cannot support the requested locale, however, they must raise a SAX
    167    exception.  Applications may request a locale change in the middle of a parse.
    168 
    169 
    170 .. method:: XMLReader.getFeature(featurename)
    171 
    172    Return the current setting for feature *featurename*.  If the feature is not
    173    recognized, :exc:`SAXNotRecognizedException` is raised. The well-known
    174    featurenames are listed in the module :mod:`xml.sax.handler`.
    175 
    176 
    177 .. method:: XMLReader.setFeature(featurename, value)
    178 
    179    Set the *featurename* to *value*. If the feature is not recognized,
    180    :exc:`SAXNotRecognizedException` is raised. If the feature or its setting is not
    181    supported by the parser, *SAXNotSupportedException* is raised.
    182 
    183 
    184 .. method:: XMLReader.getProperty(propertyname)
    185 
    186    Return the current setting for property *propertyname*. If the property is not
    187    recognized, a :exc:`SAXNotRecognizedException` is raised. The well-known
    188    propertynames are listed in the module :mod:`xml.sax.handler`.
    189 
    190 
    191 .. method:: XMLReader.setProperty(propertyname, value)
    192 
    193    Set the *propertyname* to *value*. If the property is not recognized,
    194    :exc:`SAXNotRecognizedException` is raised. If the property or its setting is
    195    not supported by the parser, *SAXNotSupportedException* is raised.
    196 
    197 
    198 .. _incremental-parser-objects:
    199 
    200 IncrementalParser Objects
    201 -------------------------
    202 
    203 Instances of :class:`IncrementalParser` offer the following additional methods:
    204 
    205 
    206 .. method:: IncrementalParser.feed(data)
    207 
    208    Process a chunk of *data*.
    209 
    210 
    211 .. method:: IncrementalParser.close()
    212 
    213    Assume the end of the document. That will check well-formedness conditions that
    214    can be checked only at the end, invoke handlers, and may clean up resources
    215    allocated during parsing.
    216 
    217 
    218 .. method:: IncrementalParser.reset()
    219 
    220    This method is called after close has been called to reset the parser so that it
    221    is ready to parse new documents. The results of calling parse or feed after
    222    close without calling reset are undefined.
    223 
    224 
    225 .. _locator-objects:
    226 
    227 Locator Objects
    228 ---------------
    229 
    230 Instances of :class:`Locator` provide these methods:
    231 
    232 
    233 .. method:: Locator.getColumnNumber()
    234 
    235    Return the column number where the current event begins.
    236 
    237 
    238 .. method:: Locator.getLineNumber()
    239 
    240    Return the line number where the current event begins.
    241 
    242 
    243 .. method:: Locator.getPublicId()
    244 
    245    Return the public identifier for the current event.
    246 
    247 
    248 .. method:: Locator.getSystemId()
    249 
    250    Return the system identifier for the current event.
    251 
    252 
    253 .. _input-source-objects:
    254 
    255 InputSource Objects
    256 -------------------
    257 
    258 
    259 .. method:: InputSource.setPublicId(id)
    260 
    261    Sets the public identifier of this :class:`InputSource`.
    262 
    263 
    264 .. method:: InputSource.getPublicId()
    265 
    266    Returns the public identifier of this :class:`InputSource`.
    267 
    268 
    269 .. method:: InputSource.setSystemId(id)
    270 
    271    Sets the system identifier of this :class:`InputSource`.
    272 
    273 
    274 .. method:: InputSource.getSystemId()
    275 
    276    Returns the system identifier of this :class:`InputSource`.
    277 
    278 
    279 .. method:: InputSource.setEncoding(encoding)
    280 
    281    Sets the character encoding of this :class:`InputSource`.
    282 
    283    The encoding must be a string acceptable for an XML encoding declaration (see
    284    section 4.3.3 of the XML recommendation).
    285 
    286    The encoding attribute of the :class:`InputSource` is ignored if the
    287    :class:`InputSource` also contains a character stream.
    288 
    289 
    290 .. method:: InputSource.getEncoding()
    291 
    292    Get the character encoding of this InputSource.
    293 
    294 
    295 .. method:: InputSource.setByteStream(bytefile)
    296 
    297    Set the byte stream (a :term:`binary file`) for this input source.
    298 
    299    The SAX parser will ignore this if there is also a character stream specified,
    300    but it will use a byte stream in preference to opening a URI connection itself.
    301 
    302    If the application knows the character encoding of the byte stream, it should
    303    set it with the setEncoding method.
    304 
    305 
    306 .. method:: InputSource.getByteStream()
    307 
    308    Get the byte stream for this input source.
    309 
    310    The getEncoding method will return the character encoding for this byte stream,
    311    or ``None`` if unknown.
    312 
    313 
    314 .. method:: InputSource.setCharacterStream(charfile)
    315 
    316    Set the character stream (a :term:`text file`) for this input source.
    317 
    318    If there is a character stream specified, the SAX parser will ignore any byte
    319    stream and will not attempt to open a URI connection to the system identifier.
    320 
    321 
    322 .. method:: InputSource.getCharacterStream()
    323 
    324    Get the character stream for this input source.
    325 
    326 
    327 .. _attributes-objects:
    328 
    329 The :class:`Attributes` Interface
    330 ---------------------------------
    331 
    332 :class:`Attributes` objects implement a portion of the :term:`mapping protocol
    333 <mapping>`, including the methods :meth:`~collections.abc.Mapping.copy`,
    334 :meth:`~collections.abc.Mapping.get`, :meth:`~object.__contains__`,
    335 :meth:`~collections.abc.Mapping.items`, :meth:`~collections.abc.Mapping.keys`,
    336 and :meth:`~collections.abc.Mapping.values`.  The following methods
    337 are also provided:
    338 
    339 
    340 .. method:: Attributes.getLength()
    341 
    342    Return the number of attributes.
    343 
    344 
    345 .. method:: Attributes.getNames()
    346 
    347    Return the names of the attributes.
    348 
    349 
    350 .. method:: Attributes.getType(name)
    351 
    352    Returns the type of the attribute *name*, which is normally ``'CDATA'``.
    353 
    354 
    355 .. method:: Attributes.getValue(name)
    356 
    357    Return the value of attribute *name*.
    358 
    359 .. getValueByQName, getNameByQName, getQNameByName, getQNames available
    360 .. here already, but documented only for derived class.
    361 
    362 
    363 .. _attributes-ns-objects:
    364 
    365 The :class:`AttributesNS` Interface
    366 -----------------------------------
    367 
    368 This interface is a subtype of the :class:`Attributes` interface (see section
    369 :ref:`attributes-objects`).  All methods supported by that interface are also
    370 available on :class:`AttributesNS` objects.
    371 
    372 The following methods are also available:
    373 
    374 
    375 .. method:: AttributesNS.getValueByQName(name)
    376 
    377    Return the value for a qualified name.
    378 
    379 
    380 .. method:: AttributesNS.getNameByQName(name)
    381 
    382    Return the ``(namespace, localname)`` pair for a qualified *name*.
    383 
    384 
    385 .. method:: AttributesNS.getQNameByName(name)
    386 
    387    Return the qualified name for a ``(namespace, localname)`` pair.
    388 
    389 
    390 .. method:: AttributesNS.getQNames()
    391 
    392    Return the qualified names of all attributes.
    393 
    394