@@ -105,6 +105,38 @@ Children are nested, and we can access specific child nodes by index::
105105 >>> root[0][1].text
106106 '2008'
107107
108+ Incremental parsing
109+ ^^^^^^^^^^^^^^^^^^^
110+
111+ It's possible to parse XML incrementally (i.e. not the whole document at once).
112+ The most powerful tool for doing this is :class: `IncrementalParser `. It does
113+ not require a blocking read to obtain the XML data, and is instead fed with
114+ data incrementally with :meth: `IncrementalParser.data_received ` calls. To get
115+ the parsed XML elements, call :meth: `IncrementalParser.events `. Here's an
116+ example::
117+
118+ >>> incparser = ET.IncrementalParser(['start', 'end'])
119+ >>> incparser.data_received('<mytag>sometext')
120+ >>> list(incparser.events())
121+ [('start', <Element 'mytag' at 0x7fba3f2a8688>)]
122+ >>> incparser.data_received(' more text</mytag>')
123+ >>> for event, elem in incparser.events():
124+ ... print(event)
125+ ... print(elem.tag, 'text=', elem.text)
126+ ...
127+ end
128+ mytag text= sometext more text
129+
130+ The obvious use case is applications that operate in an asynchronous fashion
131+ where the XML data is being received from a socket or read incrementally from
132+ some storage device. In such cases, blocking reads are unacceptable.
133+
134+ Because it's so flexible, :class: `IncrementalParser ` can be inconvenient
135+ to use for simpler use-cases. If you don't mind your application blocking on
136+ reading XML data but would still like to have incremental parsing capabilities,
137+ take a look at :func: `iterparse `. It can be useful when you're reading a large
138+ XML document and don't want to hold it wholly in memory.
139+
108140Finding interesting elements
109141^^^^^^^^^^^^^^^^^^^^^^^^^^^^
110142
@@ -840,7 +872,6 @@ QName Objects
840872IncrementalParser Objects
841873^^^^^^^^^^^^^^^^^^^^^^^^^
842874
843-
844875.. class :: IncrementalParser(events=None, parser=None)
845876
846877 An incremental, event-driven parser suitable for non-blocking applications.
@@ -864,7 +895,9 @@ IncrementalParser Objects
864895 Iterate over the events which have been encountered in the data fed
865896 to the parser. This method yields ``(event, elem) `` pairs, where
866897 *event * is a string representing the type of event (e.g. ``"end" ``)
867- and *elem * is the encountered :class: `Element ` object.
898+ and *elem * is the encountered :class: `Element ` object. Events
899+ provided in a previous call to :meth: `events ` will not be yielded
900+ again.
868901
869902 .. note ::
870903
0 commit comments