XML - attributes query

Newt newt_e at blueyonder.co.uk
Sat Mar 1 08:45:57 EST 2003


Hi all,

I'm trying to use the following code fragment tp pick up some attributes
from an XML element.

doc = xml.dom.minidom.parse("gard_skills_ele.xml")

for s in doc.getElementsByTagName("skill"):
    print s.childNodes.length
    for c in s.childNodes:
        print "--> " + c.nodeName
    print "--> Has " + str(s.attributes.length) + " attributes"
    print str(s.attributes.getNamedItem("name"))

The xml input file looks like:

<xml>
<skill name="Art" points="1" />
<skill name="Calligraphy" points="1" />
<skill name="Ettiquette" points="1" />
</xml>

When I run the code, I get the following error:

Traceback (most recent call last):
  File "D:\py_stuff\test_xml.py", line 22, in ?
    print str(s.attributes.getNamedItem("name"))
AttributeError: NamedNodeMap instance has no attribute 'getNamedItem'

The book I'm using (Python and XML) says that NamedNodeMap does support
getNamedItem.
Has anyone tried to use this?

Newt






More information about the Python-list mailing list