xml.sax
— Support for SAX2 parsers支持SAX2解析器¶
Source code: Lib/xml/sax/__init__.py
The xml.sax
package provides a number of modules which implement the Simple API for XML (SAX) interface for Python. xml.sax
包提供了许多模块,这些模块实现了Python的Simple API for XML(SAX)接口。The package itself provides the SAX exceptions and the convenience functions which will be most used by users of the SAX API.包本身提供SAX异常和SAXAPI用户最常用的方便函数。
Warning
The xml.sax
module is not secure against maliciously constructed data. xml.sax
模块对恶意构建的数据不安全。If you need to parse untrusted or unauthenticated data see XML vulnerabilities.如果需要解析不受信任或未经验证的数据,请参阅XML漏洞。
Changed in version 3.7.1:版本3.7.1中更改: The SAX parser no longer processes general external entities by default to increase security. 默认情况下,SAX解析器不再处理一般的外部实体以提高安全性。Before, the parser created network connections to fetch remote files or loaded local files from the file system for DTD and entities. 之前,解析器创建了网络连接,以从文件系统中获取远程文件或加载本地文件以获取DTD和实体。The feature can be enabled again with method 可以对解析器对象和参数setFeature()
on the parser object and argument feature_external_ges
.feature_external_ges
使用方法setFeature()
再次启用该功能。
The convenience functions are:便利功能包括:
-
xml.sax.
make_parser
(parser_list=[])¶ Create and return a SAX创建并返回SAXXMLReader
object.XMLReader
对象。The first parser found will be used.将使用找到的第一个解析器。If parser_list is provided, it must be an iterable of strings which name modules that have a function named如果提供了parser_list,则它必须是一个字符串的可迭代字符串,这些字符串为具有名为create_parser()
.create_parser()
函数的模块命名。Modules listed in parser_list will be used before modules in the default list of parsers.parser_list中列出的模块将在默认解析器列表中的模块之前使用。Changed in version 3.8:版本3.8中更改:The parser_list argument can be any iterable, not just a list.parser_list参数可以是任何可迭代的,而不仅仅是列表。
-
xml.sax.
parse
(filename_or_stream, handler, error_handler=handler.ErrorHandler())¶ Create a SAX parser and use it to parse a document.创建SAX解析器并使用它解析文档。The document, passed in as filename_or_stream, can be a filename or a file object.作为filename_or_stream传入的文档可以是文件名或文件对象。The handler parameter needs to be a SAXhandler参数需要是ContentHandler
instance.ContentHandler
实例。If error_handler is given, it must be a SAX如果给定了error_handler,则它必须是SAXErrorHandler
instance; if omitted,SAXParseException
will be raised on all errors.ErrorHandler
实例;如果省略,将在所有错误上引发SAXParseException
。There is no return value; all work must be done by the handler passed in.没有返回值;所有工作都必须由传入的handler完成。
-
xml.sax.
parseString
(string, handler, error_handler=handler.ErrorHandler())¶ Similar to类似于parse()
, but parses from a buffer string received as a parameter.parse()
,但从作为参数接收的缓冲区string进行解析。string must be astring必须是str
instance or a bytes-like object.str
实例或类字节对象。
A typical SAX application uses three kinds of objects: readers, handlers and input sources. 典型的SAX应用程序使用三种对象:读取器、处理程序和输入源。“Reader” in this context is another term for parser, i.e. some piece of code that reads the bytes or characters from the input source, and produces a sequence of events. 本文中的“Reader”是解析器的另一个术语,即从输入源读取字节或字符并生成一系列事件的某段代码。The events then get distributed to the handler objects, i.e. the reader invokes a method on the handler. 然后将事件分发到处理程序对象,即读取器调用处理程序上的方法。A SAX application must therefore obtain a reader object, create or open the input sources, create the handlers, and connect these objects all together. 因此,SAX应用程序必须获得读取器对象,创建或打开输入源,创建处理程序,并将这些对象连接在一起。As the final step of preparation, the reader is called to parse the input. During parsing, methods on the handler objects are called based on structural and syntactic events from the input data.作为准备的最后一步,调用读取器来解析输入。在解析过程中,基于输入数据中的结构和语法事件调用处理程序对象上的方法。
For these objects, only the interfaces are relevant; they are normally not instantiated by the application itself. 对于这些对象,只有接口是相关的;它们通常不由应用程序本身实例化。Since Python does not have an explicit notion of interface, they are formally introduced as classes, but applications may use implementations which do not inherit from the provided classes. 由于Python没有明确的接口概念,所以它们被正式引入为类,但应用程序可能使用不从提供的类继承的实现。The InputSource
, Locator
, Attributes
, AttributesNS
, and XMLReader
interfaces are defined in the module xml.sax.xmlreader
. InputSource
、Locator
、Attributes
、AttributesNS
和XMLReader
接口在模块xmlsaxxmlreader中定义。The handler interfaces are defined in 处理程序接口在xml.sax.handler
. xml.sax.handler
中定义。For convenience, 为了方便起见,InputSource
(which is often instantiated directly) and the handler classes are also available from xml.sax
. InputSource
(通常直接实例化)和处理程序类也可以从xmlsax获得。These interfaces are described below.这些接口如下所述。
In addition to these classes, 除了这些类之外,xml.sax
provides the following exception classes.xml.sax
还提供了以下异常类。
-
exception
xml.sax.
SAXException
(msg, exception=None)¶ Encapsulate an XML error or warning.封装XML错误或警告。This class can contain basic error or warning information from either the XML parser or the application: it can be subclassed to provide additional functionality or to add localization.该类可以包含来自XML解析器或应用程序的基本错误或警告信息:可以对其进行子类化以提供附加功能或添加本地化。Note that although the handlers defined in the请注意,虽然ErrorHandler
interface receive instances of this exception, it is not required to actually raise the exception — it is also useful as a container for information.ErrorHandler
接口中定义的处理程序接收此异常的实例,但不需要实际引发异常-它作为信息容器也很有用。When instantiated, msg should be a human-readable description of the error.实例化时,msg应该是错误的可读描述。The optional exception parameter, if given, should be如果给定了可选的exception参数,则该参数应为None
or an exception that was caught by the parsing code and is being passed along as information.None
或由解析代码捕获并作为信息传递的异常。This is the base class for the other SAX exception classes.这是其他SAX异常类的基类。
-
exception
xml.sax.
SAXParseException
(msg, exception, locator)¶ Subclass of分析错误时引发SAXException
raised on parse errors.SAXException
的子类。Instances of this class are passed to the methods of the SAX此类的实例被传递给SAXErrorHandler
interface to provide information about the parse error.ErrorHandler
接口的方法,以提供有关解析错误的信息。This class supports the SAX此类支持SAXLocator
interface as well as theSAXException
interface.Locator
接口以及SAXException
接口。
-
exception
xml.sax.
SAXNotRecognizedException
(msg, exception=None)¶ Subclass ofSAXSAXException
raised when a SAXXMLReader
is confronted with an unrecognized feature or property.XMLReader
遇到无法识别的特性或属性时引发的SAXException
子类。SAX applications and extensions may use this class for similar purposes.SAX应用程序和扩展可以出于类似目的使用此类。
-
exception
xml.sax.
SAXNotSupportedException
(msg, exception=None)¶ Subclass of当SAXSAXException
raised when a SAXXMLReader
is asked to enable a feature that is not supported, or to set a property to a value that the implementation does not support.SAXException
被要求启用不受支持的功能,或将属性设置为实现不支持的值时,引发SAXException
的子类。SAX applications and extensions may use this class for similar purposes.SAX应用程序和扩展可以出于类似目的使用此类。
See also
- SAX: The Simple API for XML
This site is the focal point for the definition of the SAX API.该站点是SAXAPI定义的焦点。It provides a Java implementation and online documentation. Links to implementations and historical information are also available.它提供了Java实现和在线文档。还提供了实现和历史信息的链接。- Module
xml.sax.handler
Definitions of the interfaces for application-provided objects.应用程序提供对象的接口定义。- Module
xml.sax.saxutils
Convenience functions for use in SAX applications.SAX应用程序中使用的方便函数。- Module
xml.sax.xmlreader
Definitions of the interfaces for parser-provided objects.解析器提供对象的接口定义。
SAXException Objects¶
The SAXException
exception class supports the following methods:SAXException
异常类支持以下方法:
-
SAXException.
getMessage
()¶ Return a human-readable message describing the error condition.返回描述错误情况的可读消息。
-
SAXException.
getException
()¶ Return an encapsulated exception object, or返回封装的异常对象,或None
.None
。