Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minidom:Get all the attributes of a selected node?

I recursively pass through all the nodes in an 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)

But I don't know how to get all the attributes names of the selected the selected node?

like image 936
Eduard Florinescu Avatar asked Oct 21 '25 07:10

Eduard Florinescu


1 Answers

You can simply access the attributes property, which is a NamedNodeMap, on which you can call items to get the string keys and values:

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'}
like image 61
phihag Avatar answered Oct 23 '25 22:10

phihag