Minidom:全力以赴所选节点的属性?

2024-04-26 07:45:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我递归地遍历XML中的所有节点:

def verify_elements_children(root):
    if root.childNodes:
        for node in root.childNodes:
            if node.nodeType == node.ELEMENT_NODE:
               if node.tagName in config_elements_children[node.parentNode.tagName]:
#                  print node.toxml()
                   verify_elements_children(node)

但我不知道如何获取所选的node的所有属性名?在


Tags: innodeforif节点defrootxml
1条回答
网友
1楼 · 发布于 2024-04-26 07:45:53

您可以简单地访问^{}属性,这是一个NamedNodeMap,在该属性上可以调用items来获取字符串键和值:

import xml.dom.minidom
n = xml.dom.minidom.parseString('<n a="1" b="2" />').documentElement
attrs = dict(n.attributes.items())
assert attrs == {'a': '1', 'b': '2'}

相关问题 更多 >