xml.sax — 支持 SAX2 剖析器

源代码: 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. The package itself provides the SAX exceptions and the convenience functions which will be most used by users of the SAX API.

警告

The xml.sax module is not secure against maliciously constructed data. If you need to parse untrusted or unauthenticated data see XML vulnerabilities .

Changed in version 3.7.1: The SAX parser no longer processes general external entities by default to increase security. Before, the parser created network connections to fetch remote files or loaded local files from the file system for DTD and entities. The feature can be enabled again with method setFeature() on the parser object and argument feature_external_ges .

The convenience functions are:

xml.sax. make_parser ( parser_list = [] )

Create and return a SAX XMLReader object. 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 create_parser() . Modules listed in parser_list will be used before modules in the default list of parsers.

3.8 版改变: The parser_list argument can be any iterable, not just a list.

xml.sax. parse ( filename_or_stream , handler , error_handler = handler.ErrorHandler() )

Create a SAX parser and use it to parse a document. The document, passed in as filename_or_stream , can be a filename or a file object. The handler parameter needs to be a SAX ContentHandler instance. If error_handler is given, it must be a SAX ErrorHandler instance; if omitted, SAXParseException will be raised on all errors. There is no return value; all work must be done by the handler passed in.

xml.sax. parseString ( string , handler , error_handler = handler.ErrorHandler() )

类似于 parse() , but parses from a buffer string received as a parameter. string 必须为 str 实例或 像字节对象 .

3.5 版改变: 添加支持 str 实例。