~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Doc/library/xml.sax.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
Import upstream version 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

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