[XML-SIG] Bug in xml.dom.minidom.NamedNodeMap ?
Fredrik Lundh
fredrik at pythonware.com
Fri Dec 16 13:45:08 CET 2005
Henry James wrote:
> According to the documentation, xml.dom.minidom.NamedNodeMap has
> "experimental methods that give this class more mapping behavior". But this
> is what happens:
>
>>>> import xml.dom.minidom
>>>> s = '<?xml version="1.0" encoding="UTF-8"?><root><node_1 attr_1="1"
>>>> attr_2="2"/><node_2/></root>'
>>>> n = xml.dom.minidom.parseString(s).documentElement.firstChild
>>>> n.attributes.keys()
> [u'attr_2', u'attr_1']
>>>> [a for a in n.attributes]
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "/usr/lib/python2.4/xml/dom/minidom.py", line 529, in __getitem__
> KeyError: 0
>
> I can't quite understand why access to a key "0" is being attempted here.
being able to iterate over certain mappings is a relatively new addition
to Python.
if an object doesn't implement the __iter__ hook required for iteration,
the for-loop will call __getitem__ until it gets an IndexError. any other
error will be propagated to the caller (like the KeyError you get in this
case).
try
[a for a in n.attributes.keys()]
instead.
</F>
More information about the XML-SIG
mailing list