Source code:Lib/xml/dom/pulldom.py
Thexml.dom.pulldom module provides a “pull parser” which can also beasked to produce DOM-accessible fragments of the document where necessary. Thebasic concept involves pulling “events” from a stream of incoming XML andprocessing them. In contrast to SAX which also employs an event-drivenprocessing model together with callbacks, the user of a pull parser isresponsible for explicitly pulling events from the stream, looping over thoseevents until either processing is finished or an error condition occurs.
Warning
Thexml.dom.pulldom module is not secure againstmaliciously constructed data. If you need to parse untrusted orunauthenticated data seeXML vulnerabilities.
Example:
fromxml.domimportpulldomdoc=pulldom.parse('sales_items.xml')forevent,nodeindoc:ifevent==pulldom.START_ELEMENTandnode.tagName=='item':ifint(node.getAttribute('price'))>50:doc.expandNode(node)print(node.toxml())
event is a constant and can be one of:
node is a object of typexml.dom.minidom.Document,xml.dom.minidom.Element orxml.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 theirdepth in the tree. In other words, one does not need to consider hierarchicalissues such as recursive searching of the document nodes, although if thecontext of elements were important, one would either need to maintain somecontext-related state (i.e. remembering where one is in the document at anygiven point) or to make use of theDOMEventStream.expandNode() methodand switch to DOM-related processing.
Subclass ofxml.sax.handler.ContentHandler.
Subclass ofxml.sax.handler.ContentHandler.
Return aDOMEventStream from the given input.stream_or_string may beeither a file name, or a file-like object.parser, if given, must be aXMLReader object. This function will change thedocument handler of theparser and activate namespace support; other parser configuration (likesetting an entity resolver) must have been done in advance.
If you have XML in a string, you can use theparseString() function instead:
Return aDOMEventStream that represents the (Unicode)string.
Default value for thebufsize parameter toparse().
The value of this variable can be changed before callingparse() andthe new value will take effect.
Return a tuple containingevent and the currentnode asxml.dom.minidom.Document if event equalsSTART_DOCUMENT,xml.dom.minidom.Element if event equalsSTART_ELEMENT orEND_ELEMENT orxml.dom.minidom.Text if event equalsCHARACTERS.The current node does not contain informations about its children, unlessexpandNode() is called.
Expands all children ofnode intonode. Example:
xml='<html><title>Foo</title> <p>Some text <div>and more</div></p> </html>'doc=pulldom.parseString(xml)forevent,nodeindoc:ifevent==pulldom.START_ELEMENTandnode.tagName=='p':# Following statement only prints '<p/>'print(node.toxml())doc.exandNode(node)# Following statement prints node with all its children '<p>Some text <div>and more</div></p>'print(node.toxml())
20.7.xml.dom.minidom — Minimal DOM implementation
20.9.xml.sax — Support for SAX2 parsers
Enter search terms or a module, class or function name.