python - 在 python 中迭代 Elementchildren

标签 python minidom

我通常会做以下事情:

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/

相关文章:

python - 如何在 Bokeh 绘图模块中使用 vbar 方法绘制分类条形图

python - Minidom:获取选中节点的所有属性?

python - 使用 minidom 修改时保留属性顺序

python - HDI : write large string xml into file (python xml. dom.minidom)

python - 从文件中获取用户名和密码(Python)

python - Dask DataFrame 聚合至中位数

linux - 如何编译 Open62541 教程中的示例 xml 文件?

python - 使用minidom python打印父标签xml的值

python - 加载库: pyinstaller :format messageW failed

python - Tkinter.text - 如何计算动态字符串的高度?