/Doc/library/xmllib.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 309 lines · 203 code · 106 blank · 0 comment · 0 complexity · 7d21fd36bbbfb877edb7a1f8aedf2e95 MD5 · raw file

  1. :mod:`xmllib` --- A parser for XML documents
  2. ============================================
  3. .. module:: xmllib
  4. :synopsis: A parser for XML documents.
  5. :deprecated:
  6. .. moduleauthor:: Sjoerd Mullender <Sjoerd.Mullender@cwi.nl>
  7. .. sectionauthor:: Sjoerd Mullender <Sjoerd.Mullender@cwi.nl>
  8. .. index::
  9. single: XML
  10. single: Extensible Markup Language
  11. .. deprecated:: 2.0
  12. Use :mod:`xml.sax` instead. The newer XML package includes full support for XML
  13. 1.0.
  14. .. versionchanged:: 1.5.2
  15. Added namespace support.
  16. This module defines a class :class:`XMLParser` which serves as the basis for
  17. parsing text files formatted in XML (Extensible Markup Language).
  18. .. class:: XMLParser()
  19. The :class:`XMLParser` class must be instantiated without arguments. [#]_
  20. This class provides the following interface methods and instance variables:
  21. .. attribute:: attributes
  22. A mapping of element names to mappings. The latter mapping maps attribute
  23. names that are valid for the element to the default value of the
  24. attribute, or if there is no default to ``None``. The default value is
  25. the empty dictionary. This variable is meant to be overridden, not
  26. extended since the default is shared by all instances of
  27. :class:`XMLParser`.
  28. .. attribute:: elements
  29. A mapping of element names to tuples. The tuples contain a function for
  30. handling the start and end tag respectively of the element, or ``None`` if
  31. the method :meth:`unknown_starttag` or :meth:`unknown_endtag` is to be
  32. called. The default value is the empty dictionary. This variable is
  33. meant to be overridden, not extended since the default is shared by all
  34. instances of :class:`XMLParser`.
  35. .. attribute:: entitydefs
  36. A mapping of entitynames to their values. The default value contains
  37. definitions for ``'lt'``, ``'gt'``, ``'amp'``, ``'quot'``, and ``'apos'``.
  38. .. method:: reset()
  39. Reset the instance. Loses all unprocessed data. This is called
  40. implicitly at the instantiation time.
  41. .. method:: setnomoretags()
  42. Stop processing tags. Treat all following input as literal input (CDATA).
  43. .. method:: setliteral()
  44. Enter literal mode (CDATA mode). This mode is automatically exited when
  45. the close tag matching the last unclosed open tag is encountered.
  46. .. method:: feed(data)
  47. Feed some text to the parser. It is processed insofar as it consists of
  48. complete tags; incomplete data is buffered until more data is fed or
  49. :meth:`close` is called.
  50. .. method:: close()
  51. Force processing of all buffered data as if it were followed by an
  52. end-of-file mark. This method may be redefined by a derived class to
  53. define additional processing at the end of the input, but the redefined
  54. version should always call :meth:`close`.
  55. .. method:: translate_references(data)
  56. Translate all entity and character references in *data* and return the
  57. translated string.
  58. .. method:: getnamespace()
  59. Return a mapping of namespace abbreviations to namespace URIs that are
  60. currently in effect.
  61. .. method:: handle_xml(encoding, standalone)
  62. This method is called when the ``<?xml ...?>`` tag is processed. The
  63. arguments are the values of the encoding and standalone attributes in the
  64. tag. Both encoding and standalone are optional. The values passed to
  65. :meth:`handle_xml` default to ``None`` and the string ``'no'``
  66. respectively.
  67. .. method:: handle_doctype(tag, pubid, syslit, data)
  68. .. index::
  69. single: DOCTYPE declaration
  70. single: Formal Public Identifier
  71. This method is called when the ``<!DOCTYPE...>`` declaration is processed.
  72. The arguments are the tag name of the root element, the Formal Public
  73. Identifier (or ``None`` if not specified), the system identifier, and the
  74. uninterpreted contents of the internal DTD subset as a string (or ``None``
  75. if not present).
  76. .. method:: handle_starttag(tag, method, attributes)
  77. This method is called to handle start tags for which a start tag handler
  78. is defined in the instance variable :attr:`elements`. The *tag* argument
  79. is the name of the tag, and the *method* argument is the function (method)
  80. which should be used to support semantic interpretation of the start tag.
  81. The *attributes* argument is a dictionary of attributes, the key being the
  82. *name* and the value being the *value* of the attribute found inside the
  83. tag's ``<>`` brackets. Character and entity references in the *value*
  84. have been interpreted. For instance, for the start tag ``<A
  85. HREF="http://www.cwi.nl/">``, this method would be called as
  86. ``handle_starttag('A', self.elements['A'][0], {'HREF':
  87. 'http://www.cwi.nl/'})``. The base implementation simply calls *method*
  88. with *attributes* as the only argument.
  89. .. method:: handle_endtag(tag, method)
  90. This method is called to handle endtags for which an end tag handler is
  91. defined in the instance variable :attr:`elements`. The *tag* argument is
  92. the name of the tag, and the *method* argument is the function (method)
  93. which should be used to support semantic interpretation of the end tag.
  94. For instance, for the endtag ``</A>``, this method would be called as
  95. ``handle_endtag('A', self.elements['A'][1])``. The base implementation
  96. simply calls *method*.
  97. .. method:: handle_data(data)
  98. This method is called to process arbitrary data. It is intended to be
  99. overridden by a derived class; the base class implementation does nothing.
  100. .. method:: handle_charref(ref)
  101. This method is called to process a character reference of the form
  102. ``&#ref;``. *ref* can either be a decimal number, or a hexadecimal number
  103. when preceded by an ``'x'``. In the base implementation, *ref* must be a
  104. number in the range 0-255. It translates the character to ASCII and calls
  105. the method :meth:`handle_data` with the character as argument. If *ref*
  106. is invalid or out of range, the method ``unknown_charref(ref)`` is called
  107. to handle the error. A subclass must override this method to provide
  108. support for character references outside of the ASCII range.
  109. .. method:: handle_comment(comment)
  110. This method is called when a comment is encountered. The *comment*
  111. argument is a string containing the text between the ``<!--`` and ``-->``
  112. delimiters, but not the delimiters themselves. For example, the comment
  113. ``<!--text-->`` will cause this method to be called with the argument
  114. ``'text'``. The default method does nothing.
  115. .. method:: handle_cdata(data)
  116. This method is called when a CDATA element is encountered. The *data*
  117. argument is a string containing the text between the ``<![CDATA[`` and
  118. ``]]>`` delimiters, but not the delimiters themselves. For example, the
  119. entity ``<![CDATA[text]]>`` will cause this method to be called with the
  120. argument ``'text'``. The default method does nothing, and is intended to
  121. be overridden.
  122. .. method:: handle_proc(name, data)
  123. This method is called when a processing instruction (PI) is encountered.
  124. The *name* is the PI target, and the *data* argument is a string
  125. containing the text between the PI target and the closing delimiter, but
  126. not the delimiter itself. For example, the instruction ``<?XML text?>``
  127. will cause this method to be called with the arguments ``'XML'`` and
  128. ``'text'``. The default method does nothing. Note that if a document
  129. starts with ``<?xml ..?>``, :meth:`handle_xml` is called to handle it.
  130. .. method:: handle_special(data)
  131. .. index:: single: ENTITY declaration
  132. This method is called when a declaration is encountered. The *data*
  133. argument is a string containing the text between the ``<!`` and ``>``
  134. delimiters, but not the delimiters themselves. For example, the entity
  135. declaration ``<!ENTITY text>`` will cause this method to be called with
  136. the argument ``'ENTITY text'``. The default method does nothing. Note
  137. that ``<!DOCTYPE ...>`` is handled separately if it is located at the
  138. start of the document.
  139. .. method:: syntax_error(message)
  140. This method is called when a syntax error is encountered. The *message*
  141. is a description of what was wrong. The default method raises a
  142. :exc:`RuntimeError` exception. If this method is overridden, it is
  143. permissible for it to return. This method is only called when the error
  144. can be recovered from. Unrecoverable errors raise a :exc:`RuntimeError`
  145. without first calling :meth:`syntax_error`.
  146. .. method:: unknown_starttag(tag, attributes)
  147. This method is called to process an unknown start tag. It is intended to
  148. be overridden by a derived class; the base class implementation does nothing.
  149. .. method:: unknown_endtag(tag)
  150. This method is called to process an unknown end tag. It is intended to be
  151. overridden by a derived class; the base class implementation does nothing.
  152. .. method:: unknown_charref(ref)
  153. This method is called to process unresolvable numeric character
  154. references. It is intended to be overridden by a derived class; the base
  155. class implementation does nothing.
  156. .. method:: unknown_entityref(ref)
  157. This method is called to process an unknown entity reference. It is
  158. intended to be overridden by a derived class; the base class
  159. implementation calls :meth:`syntax_error` to signal an error.
  160. .. seealso::
  161. `Extensible Markup Language (XML) 1.0 <http://www.w3.org/TR/REC-xml>`_
  162. The XML specification, published by the World Wide Web Consortium (W3C), defines
  163. the syntax and processor requirements for XML. References to additional
  164. material on XML, including translations of the specification, are available at
  165. http://www.w3.org/XML/.
  166. `Python and XML Processing <http://www.python.org/topics/xml/>`_
  167. The Python XML Topic Guide provides a great deal of information on using XML
  168. from Python and links to other sources of information on XML.
  169. `SIG for XML Processing in Python <http://www.python.org/sigs/xml-sig/>`_
  170. The Python XML Special Interest Group is developing substantial support for
  171. processing XML from Python.
  172. .. _xml-namespace:
  173. XML Namespaces
  174. --------------
  175. .. index:: pair: XML; namespaces
  176. This module has support for XML namespaces as defined in the XML Namespaces
  177. proposed recommendation.
  178. Tag and attribute names that are defined in an XML namespace are handled as if
  179. the name of the tag or element consisted of the namespace (the URL that defines
  180. the namespace) followed by a space and the name of the tag or attribute. For
  181. instance, the tag ``<html xmlns='http://www.w3.org/TR/REC-html40'>`` is treated
  182. as if the tag name was ``'http://www.w3.org/TR/REC-html40 html'``, and the tag
  183. ``<html:a href='http://frob.com'>`` inside the above mentioned element is
  184. treated as if the tag name were ``'http://www.w3.org/TR/REC-html40 a'`` and the
  185. attribute name as if it were ``'http://www.w3.org/TR/REC-html40 href'``.
  186. An older draft of the XML Namespaces proposal is also recognized, but triggers a
  187. warning.
  188. .. seealso::
  189. `Namespaces in XML <http://www.w3.org/TR/REC-xml-names/>`_
  190. This World Wide Web Consortium recommendation describes the proper syntax and
  191. processing requirements for namespaces in XML.
  192. .. rubric:: Footnotes
  193. .. [#] Actually, a number of keyword arguments are recognized which influence the
  194. parser to accept certain non-standard constructs. The following keyword
  195. arguments are currently recognized. The defaults for all of these is ``0``
  196. (false) except for the last one for which the default is ``1`` (true).
  197. *accept_unquoted_attributes* (accept certain attribute values without requiring
  198. quotes), *accept_missing_endtag_name* (accept end tags that look like ``</>``),
  199. *map_case* (map upper case to lower case in tags and attributes), *accept_utf8*
  200. (allow UTF-8 characters in input; this is required according to the XML
  201. standard, but Python does not as yet deal properly with these characters, so
  202. this is not the default), *translate_attribute_references* (don't attempt to
  203. translate character and entity references in attribute values).