xml.dom.pulldom.rst 4.34 KB

:mod:`xml.dom.pulldom` --- Support for building partial DOM trees

Source code: :source:`Lib/xml/dom/pulldom.py`


The :mod:`xml.dom.pulldom` module provides a "pull parser" which can also be asked to produce DOM-accessible fragments of the document where necessary. The basic concept involves pulling "events" from a stream of incoming XML and processing them. In contrast to SAX which also employs an event-driven processing model together with callbacks, the user of a pull parser is responsible for explicitly pulling events from the stream, looping over those events until either processing is finished or an error condition occurs.

Example:

from xml.dom import pulldom

doc = pulldom.parse('sales_items.xml')
for event, node in doc:
    if event == pulldom.START_ELEMENT and node.tagName == 'item':
        if int(node.getAttribute('price')) > 50:
            doc.expandNode(node)
            print(node.toxml())

event is a constant and can be one of:

node is a object of type :class:`xml.dom.minidom.Document`, :class:`xml.dom.minidom.Element` or :class:`xml.dom.minidom.Text`.

Since the document is treated as a "flat" stream of events, the document "tree" is implicitly traversed and the desired elements are found regardless of their depth in the tree. In other words, one does not need to consider hierarchical issues such as recursive searching of the document nodes, although if the context of elements were important, one would either need to maintain some context-related state (i.e. remembering where one is in the document at any given point) or to make use of the :func:`DOMEventStream.expandNode` method and switch to DOM-related processing.

Subclass of :class:`xml.sax.handler.ContentHandler`.

Subclass of :class:`xml.sax.handler.ContentHandler`.

If you have XML in a string, you can use the :func:`parseString` function instead:

DOMEventStream Objects