• xml.dom.pulldom —- Support for building partial DOM trees
    • DOMEventStream Objects

    xml.dom.pulldom —- Support for building partial DOM trees

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


    The xml.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.

    警告

    The xml.dom.pulldom module is not secure againstmaliciously constructed data. If you need to parse untrusted orunauthenticated data see XML 漏洞.

    在 3.7.1 版更改: The SAX parser no longer processes general external entities by default toincrease security by default. To enable processing of external entities,pass a custom parser instance in:

    1. from xml.dom.pulldom import parse
    2. from xml.sax import make_parser
    3. from xml.sax.handler import feature_external_ges
    4.  
    5. parser = make_parser()
    6. parser.setFeature(feature_external_ges, True)
    7. parse(filename, parser=parser)

    示例:

    1. from xml.dom import pulldom
    2.  
    3. doc = pulldom.parse('sales_items.xml')
    4. for event, node in doc:
    5. if event == pulldom.START_ELEMENT and node.tagName == 'item':
    6. if int(node.getAttribute('price')) > 50:
    7. doc.expandNode(node)
    8. print(node.toxml())

    event is a constant and can be one of:

    • START_ELEMENT

    • END_ELEMENT

    • COMMENT

    • START_DOCUMENT

    • END_DOCUMENT

    • CHARACTERS

    • PROCESSING_INSTRUCTION

    • IGNORABLE_WHITESPACE

    node is an object of type xml.dom.minidom.Document,xml.dom.minidom.Element or 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 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 the DOMEventStream.expandNode() methodand switch to DOM-related processing.

    • class xml.dom.pulldom.PullDom(documentFactory=None)
    • Subclass of xml.sax.handler.ContentHandler.

    • class xml.dom.pulldom.SAX2DOM(documentFactory=None)

    • Subclass of xml.sax.handler.ContentHandler.

    • xml.dom.pulldom.parse(stream_or_string, parser=None, bufsize=None)

    • Return a DOMEventStream from the given input. stream_or_string may beeither a file name, or a file-like object. parser, if given, must be anXMLReader 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 the parseString() function instead:

    • xml.dom.pulldom.parseString(string, parser=None)
    • Return a DOMEventStream that represents the (Unicode) string.

    • xml.dom.pulldom.default_bufsize

    • Default value for the bufsize parameter to parse().

    The value of this variable can be changed before calling parse() andthe new value will take effect.

    DOMEventStream Objects

    • class xml.dom.pulldom.DOMEventStream(stream, parser, bufsize)

    3.8 版后已移除: Support for sequence protocol is deprecated.

    • getEvent()
    • Return a tuple containing event and the current node asxml.dom.minidom.Document if event equals START_DOCUMENT,xml.dom.minidom.Element if event equals START_ELEMENT orEND_ELEMENT or xml.dom.minidom.Text if event equalsCHARACTERS.The current node does not contain information about its children, unlessexpandNode() is called.

    • expandNode(node)

    • Expands all children of node into node. Example:
    1. from xml.dom import pulldom
    2.  
    3. xml = '<html><title>Foo</title> <p>Some text <div>and more</div></p> </html>'
    4. doc = pulldom.parseString(xml)
    5. for event, node in doc:
    6. if event == pulldom.START_ELEMENT and node.tagName == 'p':
    7. # Following statement only prints '<p/>'
    8. print(node.toxml())
    9. doc.expandNode(node)
    10. # Following statement prints node with all its children '<p>Some text <div>and more</div></p>'
    11. print(node.toxml())
    • reset()