(Python 3.2)
I'm using etree
to parse some XML. To do this, I'm recursively iterating through the document with iterdescendants()
. So, something like:
for elem in doc.iterdescendants():
if elem.tag == "tag":
pass # Further pr开发者_StackOverflow社区ocessing
Sometimes, I process a parent tag that contains children that I want to prevent from being processed in a later recursion. Is it ok to destroy the children?
In my initial testing, I've tried:
for child in elem.getchildren(): child.clear()
For some reason, this results in the element immediately after elem
from being processed. It's like the element gets removed as well.
I then tried this, which works (in that it removes the parent and its children, but doesn't result in any subsequent siblings of the parent from being skipped/affected as well):
elem.clear()
Can anyone shed some light on this? Thanks,
I have the following code in place of yours and it seems to work, deleting all the child elements. I use iterfind to find all descendants with the tag and delete them.
for element in doc.iterfind('.//%s'%tag):
element.getparent().remove(element)
精彩评论