开发者

When using iterdescendants() on an etree, is it ok to modify the tree?

(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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜