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