Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`xml.dom.pulldom` --- Support for building partial DOM trees |
| 2 | ================================================================= |
| 3 | |
| 4 | .. module:: xml.dom.pulldom |
| 5 | :synopsis: Support for building partial DOM trees from SAX events. |
| 6 | .. moduleauthor:: Paul Prescod <paul@prescod.net> |
| 7 | |
Raymond Hettinger | 3029aff | 2011-02-10 08:09:36 +0000 | [diff] [blame] | 8 | **Source code:** :source:`Lib/xml/dom/pulldom.py` |
| 9 | |
| 10 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
Eli Bendersky | 3fb05a9 | 2012-03-16 14:37:14 +0200 | [diff] [blame] | 12 | The :mod:`xml.dom.pulldom` module provides a "pull parser" which can also be |
| 13 | asked to produce DOM-accessible fragments of the document where necessary. The |
| 14 | basic concept involves pulling "events" from a stream of incoming XML and |
| 15 | processing them. In contrast to SAX which also employs an event-driven |
| 16 | processing model together with callbacks, the user of a pull parser is |
| 17 | responsible for explicitly pulling events from the stream, looping over those |
| 18 | events until either processing is finished or an error condition occurs. |
| 19 | |
Christian Heimes | 7380a67 | 2013-03-26 17:35:55 +0100 | [diff] [blame] | 20 | |
| 21 | .. warning:: |
| 22 | |
| 23 | The :mod:`xml.dom.pulldom` module is not secure against |
| 24 | maliciously constructed data. If you need to parse untrusted or |
| 25 | unauthenticated data see :ref:`xml-vulnerabilities`. |
| 26 | |
| 27 | |
Eli Bendersky | 3fb05a9 | 2012-03-16 14:37:14 +0200 | [diff] [blame] | 28 | Example:: |
| 29 | |
| 30 | from xml.dom import pulldom |
| 31 | |
| 32 | doc = pulldom.parse('sales_items.xml') |
| 33 | for event, node in doc: |
| 34 | if event == pulldom.START_ELEMENT and node.tagName == 'item': |
| 35 | if int(node.getAttribute('price')) > 50: |
| 36 | doc.expandNode(node) |
| 37 | print(node.toxml()) |
| 38 | |
| 39 | ``event`` is a constant and can be one of: |
| 40 | |
| 41 | * :data:`START_ELEMENT` |
| 42 | * :data:`END_ELEMENT` |
| 43 | * :data:`COMMENT` |
| 44 | * :data:`START_DOCUMENT` |
| 45 | * :data:`END_DOCUMENT` |
| 46 | * :data:`CHARACTERS` |
| 47 | * :data:`PROCESSING_INSTRUCTION` |
| 48 | * :data:`IGNORABLE_WHITESPACE` |
| 49 | |
| 50 | ``node`` is a object of type :class:`xml.dom.minidom.Document`, |
| 51 | :class:`xml.dom.minidom.Element` or :class:`xml.dom.minidom.Text`. |
| 52 | |
| 53 | Since the document is treated as a "flat" stream of events, the document "tree" |
| 54 | is implicitly traversed and the desired elements are found regardless of their |
Eli Bendersky | 969b8da | 2012-03-16 16:49:58 +0200 | [diff] [blame] | 55 | depth in the tree. In other words, one does not need to consider hierarchical |
| 56 | issues such as recursive searching of the document nodes, although if the |
| 57 | context of elements were important, one would either need to maintain some |
| 58 | context-related state (i.e. remembering where one is in the document at any |
| 59 | given point) or to make use of the :func:`DOMEventStream.expandNode` method |
| 60 | and switch to DOM-related processing. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | |
| 62 | |
Eli Bendersky | 3fb05a9 | 2012-03-16 14:37:14 +0200 | [diff] [blame] | 63 | .. class:: PullDom(documentFactory=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 | |
Eli Bendersky | 3fb05a9 | 2012-03-16 14:37:14 +0200 | [diff] [blame] | 65 | Subclass of :class:`xml.sax.handler.ContentHandler`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
| 67 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 68 | .. class:: SAX2DOM(documentFactory=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | |
Eli Bendersky | 3fb05a9 | 2012-03-16 14:37:14 +0200 | [diff] [blame] | 70 | Subclass of :class:`xml.sax.handler.ContentHandler`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
| 72 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 73 | .. function:: parse(stream_or_string, parser=None, bufsize=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
Eli Bendersky | 3fb05a9 | 2012-03-16 14:37:14 +0200 | [diff] [blame] | 75 | Return a :class:`DOMEventStream` from the given input. *stream_or_string* may be |
| 76 | either a file name, or a file-like object. *parser*, if given, must be a |
Serhiy Storchaka | 15e6590 | 2013-08-29 10:28:44 +0300 | [diff] [blame] | 77 | :class:`~xml.sax.xmlreader.XMLReader` object. This function will change the |
| 78 | document handler of the |
Eli Bendersky | 3fb05a9 | 2012-03-16 14:37:14 +0200 | [diff] [blame] | 79 | parser and activate namespace support; other parser configuration (like |
| 80 | setting an entity resolver) must have been done in advance. |
| 81 | |
| 82 | If you have XML in a string, you can use the :func:`parseString` function instead: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 84 | .. function:: parseString(string, parser=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | |
Eli Bendersky | 969b8da | 2012-03-16 16:49:58 +0200 | [diff] [blame] | 86 | Return a :class:`DOMEventStream` that represents the (Unicode) *string*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | |
| 88 | .. data:: default_bufsize |
| 89 | |
| 90 | Default value for the *bufsize* parameter to :func:`parse`. |
| 91 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 92 | The value of this variable can be changed before calling :func:`parse` and |
| 93 | the new value will take effect. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 95 | .. _domeventstream-objects: |
| 96 | |
| 97 | DOMEventStream Objects |
| 98 | ---------------------- |
| 99 | |
Eli Bendersky | 3fb05a9 | 2012-03-16 14:37:14 +0200 | [diff] [blame] | 100 | .. class:: DOMEventStream(stream, parser, bufsize) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 101 | |
| 102 | |
Eli Bendersky | 969b8da | 2012-03-16 16:49:58 +0200 | [diff] [blame] | 103 | .. method:: getEvent() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | |
Eli Bendersky | 3fb05a9 | 2012-03-16 14:37:14 +0200 | [diff] [blame] | 105 | Return a tuple containing *event* and the current *node* as |
Eli Bendersky | 969b8da | 2012-03-16 16:49:58 +0200 | [diff] [blame] | 106 | :class:`xml.dom.minidom.Document` if event equals :data:`START_DOCUMENT`, |
| 107 | :class:`xml.dom.minidom.Element` if event equals :data:`START_ELEMENT` or |
| 108 | :data:`END_ELEMENT` or :class:`xml.dom.minidom.Text` if event equals |
| 109 | :data:`CHARACTERS`. |
Eli Bendersky | 3fb05a9 | 2012-03-16 14:37:14 +0200 | [diff] [blame] | 110 | The current node does not contain informations about its children, unless |
| 111 | :func:`expandNode` is called. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |
Eli Bendersky | 969b8da | 2012-03-16 16:49:58 +0200 | [diff] [blame] | 113 | .. method:: expandNode(node) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 | |
Eli Bendersky | 3fb05a9 | 2012-03-16 14:37:14 +0200 | [diff] [blame] | 115 | Expands all children of *node* into *node*. Example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 116 | |
Eli Bendersky | 3fb05a9 | 2012-03-16 14:37:14 +0200 | [diff] [blame] | 117 | xml = '<html><title>Foo</title> <p>Some text <div>and more</div></p> </html>' |
| 118 | doc = pulldom.parseString(xml) |
| 119 | for event, node in doc: |
| 120 | if event == pulldom.START_ELEMENT and node.tagName == 'p': |
| 121 | # Following statement only prints '<p/>' |
| 122 | print(node.toxml()) |
| 123 | doc.exandNode(node) |
| 124 | # Following statement prints node with all its children '<p>Some text <div>and more</div></p>' |
| 125 | print(node.toxml()) |
| 126 | |
| 127 | .. method:: DOMEventStream.reset() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |