/Doc/library/xml.sax.utils.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 85 lines · 55 code · 30 blank · 0 comment · 0 complexity · 283f1f48c29e94f646cef92f0d73b0d9 MD5 · raw file

  1. :mod:`xml.sax.saxutils` --- SAX Utilities
  2. =========================================
  3. .. module:: xml.sax.saxutils
  4. :synopsis: Convenience functions and classes for use with SAX.
  5. .. moduleauthor:: Lars Marius Garshol <larsga@garshol.priv.no>
  6. .. sectionauthor:: Martin v. Lรถwis <martin@v.loewis.de>
  7. .. versionadded:: 2.0
  8. The module :mod:`xml.sax.saxutils` contains a number of classes and functions
  9. that are commonly useful when creating SAX applications, either in direct use,
  10. or as base classes.
  11. .. function:: escape(data[, entities])
  12. Escape ``'&'``, ``'<'``, and ``'>'`` in a string of data.
  13. You can escape other strings of data by passing a dictionary as the optional
  14. *entities* parameter. The keys and values must all be strings; each key will be
  15. replaced with its corresponding value. The characters ``'&'``, ``'<'`` and
  16. ``'>'`` are always escaped, even if *entities* is provided.
  17. .. function:: unescape(data[, entities])
  18. Unescape ``'&amp;'``, ``'&lt;'``, and ``'&gt;'`` in a string of data.
  19. You can unescape other strings of data by passing a dictionary as the optional
  20. *entities* parameter. The keys and values must all be strings; each key will be
  21. replaced with its corresponding value. ``'&amp'``, ``'&lt;'``, and ``'&gt;'``
  22. are always unescaped, even if *entities* is provided.
  23. .. versionadded:: 2.3
  24. .. function:: quoteattr(data[, entities])
  25. Similar to :func:`escape`, but also prepares *data* to be used as an
  26. attribute value. The return value is a quoted version of *data* with any
  27. additional required replacements. :func:`quoteattr` will select a quote
  28. character based on the content of *data*, attempting to avoid encoding any
  29. quote characters in the string. If both single- and double-quote characters
  30. are already in *data*, the double-quote characters will be encoded and *data*
  31. will be wrapped in double-quotes. The resulting string can be used directly
  32. as an attribute value::
  33. >>> print "<element attr=%s>" % quoteattr("ab ' cd \" ef")
  34. <element attr="ab ' cd &quot; ef">
  35. This function is useful when generating attribute values for HTML or any SGML
  36. using the reference concrete syntax.
  37. .. versionadded:: 2.2
  38. .. class:: XMLGenerator([out[, encoding]])
  39. This class implements the :class:`ContentHandler` interface by writing SAX
  40. events back into an XML document. In other words, using an :class:`XMLGenerator`
  41. as the content handler will reproduce the original document being parsed. *out*
  42. should be a file-like object which will default to *sys.stdout*. *encoding* is
  43. the encoding of the output stream which defaults to ``'iso-8859-1'``.
  44. .. class:: XMLFilterBase(base)
  45. This class is designed to sit between an :class:`XMLReader` and the client
  46. application's event handlers. By default, it does nothing but pass requests up
  47. to the reader and events on to the handlers unmodified, but subclasses can
  48. override specific methods to modify the event stream or the configuration
  49. requests as they pass through.
  50. .. function:: prepare_input_source(source[, base])
  51. This function takes an input source and an optional base URL and returns a fully
  52. resolved :class:`InputSource` object ready for reading. The input source can be
  53. given as a string, a file-like object, or an :class:`InputSource` object;
  54. parsers will use this function to implement the polymorphic *source* argument to
  55. their :meth:`parse` method.