xml.sax.handler — Base classes for SAX handlers — Python 3.16.0a1 documentation
Navigation index modules | next | previous | Python » 3.16.0a1 Documentation » The Python standard library » Structured Markup Processing Tools » xml.sax.handler — Base classes for SAX handlers | Theme | xml.sax.handler — Base classes for SAX handlers ¶ Source code: Lib/xml/sax/handler.py The SAX API defines five kinds of handlers: content handlers, DTD handlers, error handlers, entity resolvers and lexical handlers.
Applications normally only need to implement those interfaces whose events they are interested in; they can implement the interfaces in a single object or in multiple objects. Handler implementations should inherit from the base classes provided in the module xml.sax.handler , so that all methods get default implementations. class xml.sax.handler. ContentHandler ¶ This is the main callback interface in SAX, and the one most important to applications.
The order of events in this interface mirrors the order of the information in the document. class xml.sax.handler. DTDHandler ¶ Handle DTD events. This interface specifies only those DTD events required for basic parsing (unparsed entities and attributes). class xml.sax.handler. EntityResolver ¶ Basic interface for resolving entities. If you create an object implementing this interface, then register the object with your Parser, the parser will call the method in your object to resolve all external entities. class xml.sax.handler.
ErrorHandler ¶ Interface used by the parser to present error and warning messages to the application. The methods of this object control whether errors are immediately converted to exceptions or are handled in some other way. class xml.sax.handler. LexicalHandler ¶ Interface used by the parser to represent low frequency events which may not be of interest to many applications. In addition to these classes, xml.sax.handler provides symbolic constants for the feature and property names. xml.sax.handler. feature_namespaces ¶ value: "http://xml.org/sax/features/namespaces" true: Perform Namespace processing. false: Optionally do not perform Namespace processing (implies namespace-prefixes; default). access: (parsing) read-only; (not parsing) read/write xml.sax.handler. feature_namespace_prefixes ¶ value: "http://xml.org/sax/features/namespace-prefixes" true: Report the original prefixed names and attributes used for Namespace declarations. false: Do not report attributes used for Namespace declarations, and optionally do not report original prefixed names (default). access: (parsing) read-only; (not parsing) read/write The parser based on xml.parsers.expat does not support this feature. xml.sax.handler. feature_string_interning ¶ value: "http://xml.org/sax/features/string-interning" true: All element names, prefixes, attribute names, Namespace URIs, and local names are interned in a dictionary (see property_interning_dict ). false: Names are not necessarily interned, although they may be (default). access: (parsing) read-only; (not parsing) read/write xml.sax.handler. feature_validation ¶ value: "http://xml.org/sax/features/validation" true: Report all validation errors (implies external-general-entities and external-parameter-entities). false: Do not report validation errors. access: (parsing) read-only; (not parsing) read/write The parser based on xml.parsers.expat does not support this feature, because Expat is a non-validating parser. xml.sax.handler. feature_external_ges ¶ Warning Enabling opens a vulnerability to external entity attacks if the parser is used with user-provided XML content.
Please reflect on your threat model before enabling this feature. value: "http://xml.org/sax/features/external-general-entities" true: Include all external general (text) entities. false: Do not include external general entities. access: (parsing) read-only; (not parsing) read/write xml.sax.handler. feature_external_pes ¶ value: "http://xml.org/sax/features/external-parameter-entities" true: Include all external parameter entities, including the external DTD subset. false: Do not include any external parameter entities, even the external DTD subset. access: (parsing) read-only; (not parsing) read/write The parser based on xml.parsers.expat does not support this feature. xml.sax.handler. all_features ¶ List of all features. xml.sax.handler. property_lexical_handler ¶ value: "http://xml.org/sax/properties/lexical-handler" data type: LexicalHandler description: An optional extension handler for lexical events like comments. access: read/write xml.sax.handler. property_declaration_handler ¶ value: "http://xml.org/sax/properties/declaration-handler" data type: an object implementing the SAX2 DeclHandler interface description: An optional extension handler for DTD-related events other than notations and unparsed entities. access: read/write No parser in the standard library supports this property, and the standard library provides no such handler. xml.sax.handler. property_dom_node ¶ value: "http://xml.org/sax/properties/dom-node" data type: xml.dom.Node description: When parsing, the current DOM node being visited if this is a DOM iterator; when not parsing, the root DOM node for iteration. access: (parsing) read-only; (not parsing) read/write No parser in the standard library supports this property. xml.sax.handler. property_xml_string ¶ value: "http://xml.org/sax/properties/xml-string" data type: Bytes description: The literal string of characters that was the source for the current event. access: read-only, and only during a handler callback xml.sax.handler. property_encoding ¶ value: "http://www.python.org/sax/properties/encoding" data type: String description: The name of the encoding to assume for input data. access: read/write No parser in the standard library supports this property. xml.sax.handler. property_interning_dict ¶ value: "http://www.python.org/sax/properties/interning-dict" data type: Dictionary description: The dictionary used to intern names, or None if names are not interned.
Setting it enables interning, as does the feature_string_interning feature. access: read/write xml.sax.handler. all_properties ¶ List of all known property names. ContentHandler Objects ¶ Users are expected to subclass ContentHandler to support their application. The following methods are called by the parser on the appropriate events in the input document: ContentHandler. setDocumentLocator ( locator ) ¶ Called by the parser to give the application a locator for locating the origin of document events.
SAX parsers are strongly encouraged (though not absolutely required) to supply a locator: if it does so, it must supply the locator to the application by invoking this method before invoking any of the other methods in the DocumentHandler interface. The locator allows the application to determine the end position of any document-related event, even if the parser is not reporting an error. Typically, the application will use this information for reporting its own errors (such as character content that does not match an application’s business rules).
The information returned by the locator is probably not sufficient for use with a search engine. Note that the locator will return correct information only during the invocation of the events in this interface. The application should not attempt to use it at any other time. ContentHandler. startDocument ( ) ¶ Receive notification of the beginning of a document. The SAX parser will invoke this method only once, before any other methods in this interface or in DTDHandler (except for setDocumentLocator() ).
ContentHandler. endDocument ( ) ¶ Receive notification of the end of a document. The SAX parser will invoke this method only once, and it will be the last method invoked during the parse. The parser shall not invoke this method until it has either abandoned parsing (because of an unrecoverable error) or reached the end of input. ContentHandler. startPrefixMapping ( prefix , uri ) ¶ Begin the scope of a prefix-U
The rest of this page is on the live site.