我递归地遍历 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)
但我不知道如何获取所选节点
的所有属性名称?
最佳答案
您可以简单地访问 attributes
property,这是一个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'}
关于python - Minidom:获取选中节点的所有属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12676328/