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