Python Glossary Русский

Python 3.4+xml.etree.ElementTreeterm

xml.etree.ElementTree.XMLPullParser

incremental parsing

A non-blocking parser: you push data in chunks with feed() and pull finished events with read_events() — for network and asynchronous code.

Syntax

parser = ET.XMLPullParser(events=None)
parser.feed(data)
for event, elem in parser.read_events(): ...

Unlike iterparse it reads nothing by itself: you decide where and when the data comes from. read_events() yields only new events; already read ones never come back. close() always returns None, not the root as XMLParser.close() does; take the root from the top element's 'end' event. Newer expat versions may hold back an unfinished token until the next feed(); 3.13 added flush().

Examples

import xml.etree.ElementTree as ET
p = ET.XMLPullParser(events=('end',))
for chunk in ['<log><e>a</e>', '<e>b</e>', '</log>']:
    p.feed(chunk)
    print([el.text for ev, el in p.read_events() if el.tag == 'e'])
# → ['a'] / ['b'] / []
print(p.close())  # → None

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy