Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72acea5

Browse files
Recommend XMLPullTarget in the section about the memory usage
1 parent a8148ba commit 72acea5

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

Doc/library/xml.etree.elementtree.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,17 @@ remove the processed children from it::
188188

189189
These examples are not universal,
190190
they only give an idea for two common cases.
191-
If you do not need a tree at all,
192-
parse with :class:`XMLParser` and a custom target instead;
193-
it is not built then, and nothing has to be removed.
191+
If you do not need the tree,
192+
parse with the :class:`XMLPullTarget` target instead:
193+
it reports the elements without building a tree,
194+
and :meth:`XMLPullParser.expand` builds the subtree of an element
195+
when it is needed::
196+
197+
it = ET.iterparse(source, events=('start', 'end'), target=ET.XMLPullTarget())
198+
for event, elem in it:
199+
if event == 'start' and elem.tag == 'record':
200+
it.expand(elem)
201+
process(elem)
194202

195203
Where *immediate* feedback through events is wanted, calling method
196204
:meth:`XMLPullParser.flush` can help reduce delay;

0 commit comments

Comments
 (0)