20.12. "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" 读取实体所需信息的封装。

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

   Applications will create objects of this class for use in the
   "XMLReader.parse()" method and for returning from
   EntityResolver.resolveEntity.

   An "InputSource" belongs to the application, the "XMLReader" is not
   allowed to modify "InputSource" objects passed to it from the
   application, although it may make copies and modify those.

class xml.sax.xmlreader.AttributesImpl(attrs)

   This is an implementation of the "Attributes" interface (see
   section The Attributes Interface).  This is a dictionary-like
   object which represents the element attributes in a
   "startElement()" call. In addition to the most useful dictionary
   operations, it supports a number of other methods as described by
   the interface. Objects of this class should be instantiated by
   readers; *attrs* must be a dictionary-like object containing a
   mapping from attribute names to attribute values.

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

   Namespace-aware variant of "AttributesImpl", which will be passed
   to "startElementNS()". It is derived from "AttributesImpl", but
   understands attribute names as two-tuples of *namespaceURI* and
   *localname*. In addition, it provides a number of methods expecting
   qualified names as they appear in the original document.  This
   class implements the "AttributesNS" interface (see section The
   AttributesNS Interface).


20.12.1. XMLReader 对象
=======================

The "XMLReader" interface supports the following methods:

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 file-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.

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.


20.12.2. 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.


20.12.3. 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.


20.12.4. 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.


20.12.5. 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*.


20.12.6. 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.
