在python中迭代Element子元素

时间:2012-02-16 12:33:24

标签: python minidom

我通常会做以下事情:

nextPreset = element.firstChild
while nextPreset != None:
    #doThings
    nextPreset = nextPreset.nextSibling

我想知道是否有类似的东西:

for child in element.children:
    #doThings

我看到了方法_get_childNodes,但它是私有的......

1 个答案:

答案 0 :(得分:4)

如果您在一个项目中多次执行此操作,则可以使用

def iterate_children(parent):
    child = parent.firstChild
    while child != None:
        yield child
        child = child.nextSibling

然后再做

for child in iterate_children(element):
    # foo
相关问题