Re: Bug in xml.dom.minidom.NamedNodeMap ?

"Fredrik Lundh" <[email protected]>
Newsgroups gmane.comp.python.xml
Message-ID <[email protected]>
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> 



_______________________________________________
XML-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/xml-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.