4

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?

Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179
  • I don't see how the question has anything to do with the code. The code traverses *elements*, yet you seem to want to examine *attributes*. – phihag Oct 01 '12 at 15:34
  • @phihag I want to get the attributes of the current node, not all attributes in the xml, so in this sense I want to know how to get all the attributes from `node` object, it has to do with the question that I need the answer in this case. – Eduard Florinescu Oct 01 '12 at 15:42
  • On a side note -- there are very frew reasosn to use mindom in the current days -- maybe your problem would be easier to tackle if you were using elementtree instead (http://docs.python.org/library/xml.etree.elementtree.html ) – jsbueno Oct 01 '12 at 16:16
  • @jsbueno everybody recommends elementree rather than minidom, it will be the next step to change it all, but I already have somethin written with minidom – Eduard Florinescu Oct 01 '12 at 17:19

1 Answers1

10

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'}
phihag
  • 278,196
  • 72
  • 453
  • 469
  • 1
    Thanks it works, or in my case `for attribute, value in node.attributes.items(): print attribute, value` I was looking in the minidom documentation instead of dom: http://docs.python.org/library/xml.dom.html#xml.dom.Node.attributes – Eduard Florinescu Oct 01 '12 at 15:49