我通常会做以下事情:
nextPreset = element.firstChild
while nextPreset != None:
#doThings
nextPreset = nextPreset.nextSibling
我想知道是否有类似的东西:
for child in element.children:
#doThings
我看到一个方法_get_childNodes
,但它是私有(private)的...
最佳答案
如果您在一个项目中多次执行此操作,则可以使用
def iterate_children(parent):
child = parent.firstChild
while child != None:
yield child
child = child.nextSibling
然后做
for child in iterate_children(element):
# foo
关于python - 在 python 中迭代 Elementchildren,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9311337/