| 1 |
Sax support |
| 2 |
=========== |
| 3 |
|
| 4 |
In this document we'll describe lxml's SAX support. lxml has support for |
| 5 |
producing SAX events for an ElementTree or Element. lxml can also turn SAX |
| 6 |
events into an ElementTree. The SAX API used by lxml is compatible with that |
| 7 |
in the Python core (xml.sax), so is useful for interfacing lxml with code that |
| 8 |
uses the Python core SAX facilities. |
| 9 |
|
| 10 |
.. contents:: |
| 11 |
.. |
| 12 |
1 Building a tree from SAX events |
| 13 |
2 Producing SAX events from an ElementTree or Element |
| 14 |
3 Interfacing with pulldom/minidom |
| 15 |
|
| 16 |
.. |
| 17 |
>>> try: from StringIO import StringIO |
| 18 |
... except ImportError: |
| 19 |
... from io import BytesIO |
| 20 |
... def StringIO(s): |
| 21 |
... if isinstance(s, str): s = s.encode("UTF-8") |
| 22 |
... return BytesIO(s) |
| 23 |
|
| 24 |
|
| 25 |
Building a tree from SAX events |
| 26 |
------------------------------- |
| 27 |
|
| 28 |
First of all, lxml has support for building a new tree given SAX events. To |
| 29 |
do this, we use the special SAX content handler defined by lxml named |
| 30 |
``lxml.sax.ElementTreeContentHandler``: |
| 31 |
|
| 32 |
.. sourcecode:: pycon |
| 33 |
|
| 34 |
>>> import lxml.sax |
| 35 |
>>> handler = lxml.sax.ElementTreeContentHandler() |
| 36 |
|
| 37 |
Now let's fire some SAX events at it: |
| 38 |
|
| 39 |
.. sourcecode:: pycon |
| 40 |
|
| 41 |
>>> handler.startElementNS((None, 'a'), 'a', {}) |
| 42 |
>>> handler.startElementNS((None, 'b'), 'b', {(None, 'foo'): 'bar'}) |
| 43 |
>>> handler.characters('Hello world') |
| 44 |
>>> handler.endElementNS((None, 'b'), 'b') |
| 45 |
>>> handler.endElementNS((None, 'a'), 'a') |
| 46 |
|
| 47 |
This constructs an equivalent tree. You can access it through the ``etree`` |
| 48 |
property of the handler: |
| 49 |
|
| 50 |
.. sourcecode:: pycon |
| 51 |
|
| 52 |
>>> tree = handler.etree |
| 53 |
>>> lxml.etree.tostring(tree.getroot()) |
| 54 |
b'<a><b foo="bar">Hello world</b></a>' |
| 55 |
|
| 56 |
By passing a ``makeelement`` function to the constructor of |
| 57 |
``ElementTreeContentHandler``, e.g. the one of a parser you configured, you |
| 58 |
can determine which element class lookup scheme should be used. |
| 59 |
|
| 60 |
|
| 61 |
Producing SAX events from an ElementTree or Element |
| 62 |
--------------------------------------------------- |
| 63 |
|
| 64 |
Let's make a tree we can generate SAX events for: |
| 65 |
|
| 66 |
.. sourcecode:: pycon |
| 67 |
|
| 68 |
>>> f = StringIO('<a><b>Text</b></a>') |
| 69 |
>>> tree = lxml.etree.parse(f) |
| 70 |
|
| 71 |
To see whether the correct SAX events are produced, we'll write a custom |
| 72 |
content handler: |
| 73 |
|
| 74 |
.. sourcecode:: pycon |
| 75 |
|
| 76 |
>>> from xml.sax.handler import ContentHandler |
| 77 |
>>> class MyContentHandler(ContentHandler): |
| 78 |
... def __init__(self): |
| 79 |
... self.a_amount = 0 |
| 80 |
... self.b_amount = 0 |
| 81 |
... self.text = None |
| 82 |
... |
| 83 |
... def startElementNS(self, name, qname, attributes): |
| 84 |
... uri, localname = name |
| 85 |
... if localname == 'a': |
| 86 |
... self.a_amount += 1 |
| 87 |
... if localname == 'b': |
| 88 |
... self.b_amount += 1 |
| 89 |
... |
| 90 |
... def characters(self, data): |
| 91 |
... self.text = data |
| 92 |
|
| 93 |
Note that it only defines the startElementNS() method and not startElement(). |
| 94 |
The SAX event generator in lxml.sax currently only supports namespace-aware |
| 95 |
processing. |
| 96 |
|
| 97 |
To test the content handler, we can produce SAX events from the tree: |
| 98 |
|
| 99 |
.. sourcecode:: pycon |
| 100 |
|
| 101 |
>>> handler = MyContentHandler() |
| 102 |
>>> lxml.sax.saxify(tree, handler) |
| 103 |
|
| 104 |
This is what we expect: |
| 105 |
|
| 106 |
.. sourcecode:: pycon |
| 107 |
|
| 108 |
>>> handler.a_amount |
| 109 |
1 |
| 110 |
>>> handler.b_amount |
| 111 |
1 |
| 112 |
>>> handler.text |
| 113 |
'Text' |
| 114 |
|
| 115 |
|
| 116 |
Interfacing with pulldom/minidom |
| 117 |
-------------------------------- |
| 118 |
|
| 119 |
lxml.sax is a simple way to interface with the standard XML support in the |
| 120 |
Python library. Note, however, that this is a one-way solution, as Python's |
| 121 |
DOM implementation cannot generate SAX events from a DOM tree. |
| 122 |
|
| 123 |
You can use xml.dom.pulldom to build a minidom from lxml: |
| 124 |
|
| 125 |
.. sourcecode:: pycon |
| 126 |
|
| 127 |
>>> from xml.dom.pulldom import SAX2DOM |
| 128 |
>>> handler = SAX2DOM() |
| 129 |
>>> lxml.sax.saxify(tree, handler) |
| 130 |
|
| 131 |
PullDOM makes the result available through the ``document`` attribute: |
| 132 |
|
| 133 |
.. sourcecode:: pycon |
| 134 |
|
| 135 |
>>> dom = handler.document |
| 136 |
>>> print(dom.firstChild.localName) |
| 137 |
a |