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

Edu*_*scu 3 python xmlnode minidom python-2.7

我以递归方式遍历以下所有节点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)
Run Code Online (Sandbox Code Playgroud)

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

phi*_*hag 6

您可以简单地访问attributes属性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'}
Run Code Online (Sandbox Code Playgroud)