Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`xml.sax` --- Support for SAX2 parsers |
| 2 | =========================================== |
| 3 | |
| 4 | .. module:: xml.sax |
| 5 | :synopsis: Package containing SAX2 base classes and convenience functions. |
| 6 | .. moduleauthor:: Lars Marius Garshol <larsga@garshol.priv.no> |
| 7 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 8 | .. sectionauthor:: Martin v. Lรถwis <martin@v.loewis.de> |
| 9 | |
| 10 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | The :mod:`xml.sax` package provides a number of modules which implement the |
| 12 | Simple API for XML (SAX) interface for Python. The package itself provides the |
| 13 | SAX exceptions and the convenience functions which will be most used by users of |
| 14 | the SAX API. |
| 15 | |
Christian Heimes | 7380a67 | 2013-03-26 17:35:55 +0100 | [diff] [blame] | 16 | |
| 17 | .. warning:: |
| 18 | |
| 19 | The :mod:`xml.sax` module is not secure against maliciously |
| 20 | constructed data. If you need to parse untrusted or unauthenticated data see |
| 21 | :ref:`xml-vulnerabilities`. |
| 22 | |
| 23 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | The convenience functions are: |
| 25 | |
| 26 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 27 | .. function:: make_parser(parser_list=[]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | |
Serhiy Storchaka | 15e6590 | 2013-08-29 10:28:44 +0300 | [diff] [blame] | 29 | Create and return a SAX :class:`~xml.sax.xmlreader.XMLReader` object. The |
| 30 | first parser found will |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | be used. If *parser_list* is provided, it must be a sequence of strings which |
| 32 | name modules that have a function named :func:`create_parser`. Modules listed |
| 33 | in *parser_list* will be used before modules in the default list of parsers. |
| 34 | |
| 35 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 36 | .. function:: parse(filename_or_stream, handler, error_handler=handler.ErrorHandler()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 37 | |
| 38 | Create a SAX parser and use it to parse a document. The document, passed in as |
| 39 | *filename_or_stream*, can be a filename or a file object. The *handler* |
Serhiy Storchaka | 15e6590 | 2013-08-29 10:28:44 +0300 | [diff] [blame] | 40 | parameter needs to be a SAX :class:`~handler.ContentHandler` instance. If |
| 41 | *error_handler* is given, it must be a SAX :class:`~handler.ErrorHandler` |
| 42 | instance; if |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | omitted, :exc:`SAXParseException` will be raised on all errors. There is no |
| 44 | return value; all work must be done by the *handler* passed in. |
| 45 | |
| 46 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 47 | .. function:: parseString(string, handler, error_handler=handler.ErrorHandler()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | |
| 49 | Similar to :func:`parse`, but parses from a buffer *string* received as a |
Serhiy Storchaka | 778db28 | 2015-04-04 10:12:26 +0300 | [diff] [blame] | 50 | parameter. *string* must be a :class:`str` instance or a |
| 51 | :term:`bytes-like object`. |
| 52 | |
| 53 | .. versionchanged:: 3.5 |
| 54 | Added support of :class:`str` instances. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | |
| 56 | A typical SAX application uses three kinds of objects: readers, handlers and |
| 57 | input sources. "Reader" in this context is another term for parser, i.e. some |
| 58 | piece of code that reads the bytes or characters from the input source, and |
| 59 | produces a sequence of events. The events then get distributed to the handler |
| 60 | objects, i.e. the reader invokes a method on the handler. A SAX application |
| 61 | must therefore obtain a reader object, create or open the input sources, create |
| 62 | the handlers, and connect these objects all together. As the final step of |
| 63 | preparation, the reader is called to parse the input. During parsing, methods on |
| 64 | the handler objects are called based on structural and syntactic events from the |
| 65 | input data. |
| 66 | |
| 67 | For these objects, only the interfaces are relevant; they are normally not |
| 68 | instantiated by the application itself. Since Python does not have an explicit |
| 69 | notion of interface, they are formally introduced as classes, but applications |
| 70 | may use implementations which do not inherit from the provided classes. The |
Serhiy Storchaka | 15e6590 | 2013-08-29 10:28:44 +0300 | [diff] [blame] | 71 | :class:`~xml.sax.xmlreader.InputSource`, :class:`~xml.sax.xmlreader.Locator`, |
| 72 | :class:`~xml.sax.xmlreader.Attributes`, :class:`~xml.sax.xmlreader.AttributesNS`, |
| 73 | and :class:`~xml.sax.xmlreader.XMLReader` interfaces are defined in the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | module :mod:`xml.sax.xmlreader`. The handler interfaces are defined in |
Serhiy Storchaka | 15e6590 | 2013-08-29 10:28:44 +0300 | [diff] [blame] | 75 | :mod:`xml.sax.handler`. For convenience, |
| 76 | :class:`~xml.sax.xmlreader.InputSource` (which is often |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | instantiated directly) and the handler classes are also available from |
| 78 | :mod:`xml.sax`. These interfaces are described below. |
| 79 | |
| 80 | In addition to these classes, :mod:`xml.sax` provides the following exception |
| 81 | classes. |
| 82 | |
| 83 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 84 | .. exception:: SAXException(msg, exception=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | |
| 86 | Encapsulate an XML error or warning. This class can contain basic error or |
| 87 | warning information from either the XML parser or the application: it can be |
| 88 | subclassed to provide additional functionality or to add localization. Note |
Serhiy Storchaka | 15e6590 | 2013-08-29 10:28:44 +0300 | [diff] [blame] | 89 | that although the handlers defined in the |
| 90 | :class:`~xml.sax.handler.ErrorHandler` interface |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | receive instances of this exception, it is not required to actually raise the |
| 92 | exception --- it is also useful as a container for information. |
| 93 | |
| 94 | When instantiated, *msg* should be a human-readable description of the error. |
| 95 | The optional *exception* parameter, if given, should be ``None`` or an exception |
| 96 | that was caught by the parsing code and is being passed along as information. |
| 97 | |
| 98 | This is the base class for the other SAX exception classes. |
| 99 | |
| 100 | |
| 101 | .. exception:: SAXParseException(msg, exception, locator) |
| 102 | |
Serhiy Storchaka | 15e6590 | 2013-08-29 10:28:44 +0300 | [diff] [blame] | 103 | Subclass of :exc:`SAXException` raised on parse errors. Instances of this |
| 104 | class are passed to the methods of the SAX |
| 105 | :class:`~xml.sax.handler.ErrorHandler` interface to provide information |
| 106 | about the parse error. This class supports the SAX |
| 107 | :class:`~xml.sax.xmlreader.Locator` interface as well as the |
| 108 | :class:`SAXException` interface. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | |
| 110 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 111 | .. exception:: SAXNotRecognizedException(msg, exception=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |
Serhiy Storchaka | 15e6590 | 2013-08-29 10:28:44 +0300 | [diff] [blame] | 113 | Subclass of :exc:`SAXException` raised when a SAX |
| 114 | :class:`~xml.sax.xmlreader.XMLReader` is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 115 | confronted with an unrecognized feature or property. SAX applications and |
| 116 | extensions may use this class for similar purposes. |
| 117 | |
| 118 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 119 | .. exception:: SAXNotSupportedException(msg, exception=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 120 | |
Serhiy Storchaka | 15e6590 | 2013-08-29 10:28:44 +0300 | [diff] [blame] | 121 | Subclass of :exc:`SAXException` raised when a SAX |
| 122 | :class:`~xml.sax.xmlreader.XMLReader` is asked to |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 123 | enable a feature that is not supported, or to set a property to a value that the |
| 124 | implementation does not support. SAX applications and extensions may use this |
| 125 | class for similar purposes. |
| 126 | |
| 127 | |
| 128 | .. seealso:: |
| 129 | |
| 130 | `SAX: The Simple API for XML <http://www.saxproject.org/>`_ |
| 131 | This site is the focal point for the definition of the SAX API. It provides a |
| 132 | Java implementation and online documentation. Links to implementations and |
| 133 | historical information are also available. |
| 134 | |
| 135 | Module :mod:`xml.sax.handler` |
| 136 | Definitions of the interfaces for application-provided objects. |
| 137 | |
| 138 | Module :mod:`xml.sax.saxutils` |
| 139 | Convenience functions for use in SAX applications. |
| 140 | |
| 141 | Module :mod:`xml.sax.xmlreader` |
| 142 | Definitions of the interfaces for parser-provided objects. |
| 143 | |
| 144 | |
| 145 | .. _sax-exception-objects: |
| 146 | |
| 147 | SAXException Objects |
| 148 | -------------------- |
| 149 | |
| 150 | The :class:`SAXException` exception class supports the following methods: |
| 151 | |
| 152 | |
| 153 | .. method:: SAXException.getMessage() |
| 154 | |
| 155 | Return a human-readable message describing the error condition. |
| 156 | |
| 157 | |
| 158 | .. method:: SAXException.getException() |
| 159 | |
| 160 | Return an encapsulated exception object, or ``None``. |
| 161 | |