the doc does warn: 'You should also avoid moving or discarding the element itself.'
but the example does exactly what I do, which is to clear the element after the 'end' event. isn't the example contradicting the warning?

>>> for event, element in etree.iterparse(StringIO(xml)):
...     # ... do something with the element
...     element.clear()                 # clean up children
...     while element.getprevious() is not None:
...         del element.getparent()[0]  # clean up preceding siblings



On Wed, Jun 13, 2012 at 9:30 PM, Stefan Behnel <stefan_ml@behnel.de> wrote:
Alon Horev, 13.06.2012 20:16:
> from lxml.etree import iterparse
>
> def safe_iterparse(*args, **kwargs):
>     for event, element in iterparse(*args, **kwargs):
>         try:
>             yield (event, element)
>         finally:
>             element.clear()

This is a known limitation of the current implementation:

http://lxml.de/parsing.html#modifying-the-tree

Stefan
_________________________________________________________________
Mailing list for the lxml Python XML toolkit - http://lxml.de/
lxml@lxml.de
https://mailman-mail5.webfaction.com/listinfo/lxml