99
1010--------------
1111
12- :mod: `xml.dom.pulldom ` allows building only selected portions of a Document
13- Object Model representation of a document from SAX events.
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.
1419
20+ Example::
1521
16- .. class :: PullDOM(documentFactory=None)
22+ from xml.dom import pulldom
1723
18- :class: `xml.sax.handler.ContentHandler ` implementation that ...
24+ doc = pulldom.parse('sales_items.xml')
25+ for event, node in doc:
26+ if event == pulldom.START_ELEMENT and node.tagName == 'item':
27+ if int(node.getAttribute('price')) > 50:
28+ doc.expandNode(node)
29+ print(node.toxml())
1930
31+ ``event `` is a constant and can be one of:
32+
33+ * :data: `START_ELEMENT `
34+ * :data: `END_ELEMENT `
35+ * :data: `COMMENT `
36+ * :data: `START_DOCUMENT `
37+ * :data: `END_DOCUMENT `
38+ * :data: `CHARACTERS `
39+ * :data: `PROCESSING_INSTRUCTION `
40+ * :data: `IGNORABLE_WHITESPACE `
41+
42+ ``node `` is a object of type :class: `xml.dom.minidom.Document `,
43+ :class: `xml.dom.minidom.Element ` or :class: `xml.dom.minidom.Text `.
44+
45+ Since the document is treated as a "flat" stream of events, the document "tree"
46+ is implicitly traversed and the desired elements are found regardless of their
47+ depth in the tree. In other words, one does not need to consider hierarchical issues
48+ such as recursive searching of the document nodes, although if the context of
49+ elements were important, one would either need to maintain some context-related
50+ state (ie. remembering where one is in the document at any given point) or to
51+ make use of the :func: `DOMEventStream.expandNode ` method and switch to DOM-related processing.
2052
21- .. class :: DOMEventStream(stream, parser, bufsize)
2253
23- ...
54+ .. class :: PullDom(documentFactory=None)
55+
56+ Subclass of :class: `xml.sax.handler.ContentHandler `.
2457
2558
2659.. class :: SAX2DOM(documentFactory=None)
2760
28- :class: `xml.sax.handler.ContentHandler ` implementation that .. .
61+ Subclass of :class: `xml.sax.handler.ContentHandler `.
2962
3063
3164.. function :: parse(stream_or_string, parser=None, bufsize=None)
3265
33- ...
66+ Return a :class: `DOMEventStream ` from the given input. *stream_or_string * may be
67+ either a file name, or a file-like object. *parser *, if given, must be a
68+ :class: `XmlReader ` object. This function will change the document handler of the
69+ parser and activate namespace support; other parser configuration (like
70+ setting an entity resolver) must have been done in advance.
71+
72+ If you have XML in a string, you can use the :func: `parseString ` function instead:
3473
3574
3675.. function :: parseString(string, parser=None)
3776
38- .. .
77+ Return a :class: ` DOMEventStream ` that represents the (unicode) * string * .
3978
4079
4180.. data :: default_bufsize
@@ -51,18 +90,31 @@ Object Model representation of a document from SAX events.
5190DOMEventStream Objects
5291----------------------
5392
93+ .. class :: DOMEventStream(stream, parser, bufsize)
5494
55- .. method :: DOMEventStream.getEvent()
56-
57- ...
5895
96+ .. method :: DOMEventStream.getEvent()
5997
60- .. method :: DOMEventStream.expandNode(node)
98+ Return a tuple containing *event * and the current *node * as
99+ :class: `xml.dom.minidom.Document ` if event equals START_DOCUMENT,
100+ :class: `xml.dom.minidom.Element ` if event equals START_ELEMENT or
101+ END_ELEMENT or :class: `xml.dom.minidom.Text ` if event equals CHARACTERS.
102+ The current node does not contain informations about its children, unless
103+ :func: `expandNode ` is called.
61104
62- ...
105+ .. method :: DOMEventStream.expandNode(node)
63106
107+ Expands all children of *node * into *node *. Example::
64108
65- .. method :: DOMEventStream.reset()
109+ xml = '<html><title>Foo</title> <p>Some text <div>and more</div></p> </html>'
110+ doc = pulldom.parseString(xml)
111+ for event, node in doc:
112+ if event == pulldom.START_ELEMENT and node.tagName == 'p':
113+ # Following statement only prints '<p/>'
114+ print(node.toxml())
115+ doc.exandNode(node)
116+ # Following statement prints node with all its children '<p>Some text <div>and more</div></p>'
117+ print(node.toxml())
66118
67- ...
119+ .. method :: DOMEventStream.reset()
68120
0 commit comments