# `xml.sax.xmlreader` --- 用于 XML 解析器的接口

源代码: Lib/xml/sax/xmlreader.py

SAX 解析器实现了 `XMLReader` 接口。 它们是在一个 Python 模块中实现的，该模块必须提供一个 `create_parser()` 函数。 该函数由 `xml.sax.make_parser()` 不带参数地发起调用来创建新的解析器对象。

class `xml.sax.xmlreader.``XMLReader`

  可由 SAX 解析器继承的基类。

class `xml.sax.xmlreader.``IncrementalParser`

  在某些情况下，最好不要一次性地解析输入源，而是在可用的时候分块送入。 请注意读取器通常不会读取整个文件，它同样也是分块读取的； 并且 `parse()` 在处理完整个文档之前不会返回。 所以如果不希望 `parse()` 出现阻塞行为则应当使用这些接口。

  当解析器被实例化时它已准备好立即开始接受来自 feed 方法的数据。 在通过调用 close 方法结束解析时 reset 方法也必须被调用以使解析器准备好接受新的数据，无论它是来自于 feed 还是使用 parse 方法。

  请注意这些方法 不可 在解析期间被调用，即在 parse 被调用之后及其返回之前。

  默认情况下，该类还使用 IncrementalParser 接口的 feed, close 和 reset 方法来实现 XMLReader 接口的 parse 方法以方便 SAX 2.0 驱动的编写者。

class `xml.sax.xmlreader.``Locator`

  用于关联一个 SAX 事件与一个文档位置的接口。 定位器对象只有在调用 DocumentHandler 的方法期间才会返回有效的结果；在其他任何时候，结果都是不可预测的。 如果信息不可用，这些方法可能返回 `None`。

class `xml.sax.xmlreader.``InputSource`(system_id=None)

  `XMLReader` 读取实体所需信息的封装。

  这个类可能包括了关于公有标识符、系统标识符、字节流（可能带有字符编码格式信息）和/或一个实体的字符流的信息。

  应用程序将创建这个类的对象以便在 `XMLReader.parse()` 方法中使用或是用于从 EntityResolver.resolveEntity 返回值。

  `InputSource` 属于应用程序，`XMLReader` 不能修改从应用程序传递给它的 `InputSource` 对象，但它可以创建副本并进行修改。

class `xml.sax.xmlreader.``AttributesImpl`(attrs)

  这是 `Attributes` 接口（参见 The Attributes Interface 一节）的具体实现。 这是一个 `startElement()` 调用中的元素属性的字典类对象。 除了最有用处的字典操作，它还支持接口所描述的一些其他方法。 该类的对象应当由读取器来实例化；attrs 必须为包含从属性名到属性值的映射的字典类对象。

class `xml.sax.xmlreader.``AttributesNSImpl`(attrs, qnames)

  可感知命名空间的 `AttributesImpl` 变体形式，它将被传递给 `startElementNS()`。 它派生自 `AttributesImpl`，但会将属性名称解读为 namespaceURI 和 localname 二元组。 此外，它还提供了一些期望接收在原始文档中出现的限定名称的方法。 这个类实现了 `AttributesNS` 接口（参见 The AttributesNS Interface 一节）。

## XMLReader 对象

`XMLReader` 接口支持下列方法:

`XMLReader.``parse`(source)

  Process an input source, producing SAX events. The source object can be a system identifier (a string identifying the input source -- typically a file name or a URL), a `pathlib.Path` or path-like object, or an `InputSource` object. When `parse()` returns, the input is completely processed, and the parser object can be discarded or reset.

  在 3.5 版更改: Added support of character streams.

  在 3.8 版更改: Added support of path-like objects.

`XMLReader.``getContentHandler`()

  Return the current `ContentHandler`.

`XMLReader.``setContentHandler`(handler)

  Set the current `ContentHandler`. If no `ContentHandler` is set, content events will be discarded.

`XMLReader.``getDTDHandler`()

  Return the current `DTDHandler`.

`XMLReader.``setDTDHandler`(handler)

  Set the current `DTDHandler`. If no `DTDHandler` is set, DTD events will be discarded.

`XMLReader.``getEntityResolver`()

  Return the current `EntityResolver`.

`XMLReader.``setEntityResolver`(handler)

  Set the current `EntityResolver`. If no `EntityResolver` is set, attempts to resolve an external entity will result in opening the system identifier for the entity, and fail if it is not available.

`XMLReader.``getErrorHandler`()

  Return the current `ErrorHandler`.

`XMLReader.``setErrorHandler`(handler)

  Set the current error handler. If no `ErrorHandler` is set, errors will be raised as exceptions, and warnings will be printed.

`XMLReader.``setLocale`(locale)

  Allow an application to set the locale for errors and warnings.

  SAX parsers are not required to provide localization for errors and warnings; if they cannot support the requested locale, however, they must raise a SAX exception. Applications may request a locale change in the middle of a parse.

`XMLReader.``getFeature`(featurename)

  Return the current setting for feature featurename. If the feature is not recognized, `SAXNotRecognizedException` is raised. The well-known featurenames are listed in the module `xml.sax.handler`.

`XMLReader.``setFeature`(featurename, value)

  Set the featurename to value. If the feature is not recognized, `SAXNotRecognizedException` is raised. If the feature or its setting is not supported by the parser, SAXNotSupportedException is raised.

`XMLReader.``getProperty`(propertyname)

  Return the current setting for property propertyname. If the property is not recognized, a `SAXNotRecognizedException` is raised. The well-known propertynames are listed in the module `xml.sax.handler`.

`XMLReader.``setProperty`(propertyname, value)

  Set the propertyname to value. If the property is not recognized, `SAXNotRecognizedException` is raised. If the property or its setting is not supported by the parser, SAXNotSupportedException is raised.

## IncrementalParser 对象

Instances of `IncrementalParser` offer the following additional methods:

`IncrementalParser.``feed`(data)

  Process a chunk of data.

`IncrementalParser.``close`()

  Assume the end of the document. That will check well-formedness conditions that can be checked only at the end, invoke handlers, and may clean up resources allocated during parsing.

`IncrementalParser.``reset`()

  This method is called after close has been called to reset the parser so that it is ready to parse new documents. The results of calling parse or feed after close without calling reset are undefined.

## Locator 对象

Instances of `Locator` provide these methods:

`Locator.``getColumnNumber`()

  Return the column number where the current event begins.

`Locator.``getLineNumber`()

  Return the line number where the current event begins.

`Locator.``getPublicId`()

  Return the public identifier for the current event.

`Locator.``getSystemId`()

  Return the system identifier for the current event.

## InputSource 对象

`InputSource.``setPublicId`(id)

  Sets the public identifier of this `InputSource`.

`InputSource.``getPublicId`()

  Returns the public identifier of this `InputSource`.

`InputSource.``setSystemId`(id)

  Sets the system identifier of this `InputSource`.

`InputSource.``getSystemId`()

  Returns the system identifier of this `InputSource`.

`InputSource.``setEncoding`(encoding)

  Sets the character encoding of this `InputSource`.

  The encoding must be a string acceptable for an XML encoding declaration (see section 4.3.3 of the XML recommendation).

  The encoding attribute of the `InputSource` is ignored if the `InputSource` also contains a character stream.

`InputSource.``getEncoding`()

  Get the character encoding of this InputSource.

`InputSource.``setByteStream`(bytefile)

  Set the byte stream (a binary file) for this input source.

  The SAX parser will ignore this if there is also a character stream specified, but it will use a byte stream in preference to opening a URI connection itself.

  If the application knows the character encoding of the byte stream, it should set it with the setEncoding method.

`InputSource.``getByteStream`()

  Get the byte stream for this input source.

  The getEncoding method will return the character encoding for this byte stream, or `None` if unknown.

`InputSource.``setCharacterStream`(charfile)

  Set the character stream (a text file) for this input source.

  If there is a character stream specified, the SAX parser will ignore any byte stream and will not attempt to open a URI connection to the system identifier.

`InputSource.``getCharacterStream`()

  Get the character stream for this input source.

## The `Attributes` Interface

`Attributes` objects implement a portion of the mapping protocol, including the methods `copy()`, `get()`, `__contains__()`, `items()`, `keys()`, and `values()`. The following methods are also provided:

`Attributes.``getLength`()

  Return the number of attributes.

`Attributes.``getNames`()

  Return the names of the attributes.

`Attributes.``getType`(name)

  Returns the type of the attribute name, which is normally `'CDATA'`.

`Attributes.``getValue`(name)

  Return the value of attribute name.

## The `AttributesNS` Interface

This interface is a subtype of the `Attributes` interface (see section The Attributes Interface). All methods supported by that interface are also available on `AttributesNS` objects.

The following methods are also available:

`AttributesNS.``getValueByQName`(name)

  Return the value for a qualified name.

`AttributesNS.``getNameByQName`(name)

  Return the `(namespace, localname)` pair for a qualified name.

`AttributesNS.``getQNameByName`(name)

  Return the qualified name for a `(namespace, localname)` pair.

`AttributesNS.``getQNames`()

  Return the qualified names of all attributes.