PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

/Doc/library/xml.sax.rst

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