views:

232

answers:

2

Getting attributes using minidom in Python, one uses the "attributes" property. e.g. node.attributes["id"].value

So if I have <a id="foo"></a>, that should give me "foo". node.attributes["id"] does not return the value of the named attribute, but an xml.dom.minidom.Attr instance. But looking at the help for Attr, by doing help('xml.dom.minidom.Attr'), nowhere is this magic "value" property mentioned. I like to learn APIs by looking at the type hierarchy, instance methods etc. Where did this "value" property come from?? Why is it not listed in the Attr class' page? The only data descriptors mentioned are isId, localName and schemaType. Its also not inherited from any superclasses. Since I'm new to Python, would some of the Python gurus enlighten?

A: 

Geez, never noticed that before. You're not kidding, node.value isn't mentioned anywhere. It is definitely being set in the code though under def __setitem__ in xml.dom.minidom.

Not sure what to say other than, it looks like you'll have to use that.

xyld
+1  A: 

The minidom is just an implementation of the xml.dom interfaces, so any docs specifically on minidom will only be about its peculiarities or limitations wrt xml.dom itself.

The xml.dom docs on Attr say, and I quote:

Attr inherits from Node, so inherits all its attributes.

The docs on Node actually name the attribute differently: nodeValue. But, indeed...:

>>> import xml.dom.minidom as xdm
>>> dom = xdm.parseString('<foo bar="baz"/>')
>>> root = dom.documentElement
>>> atr = root.getAttributeNode('bar')
>>> atr.nodeValue
u'baz'

The fact that the documented nodeValue attribute has an _un_documented alias value may be considered unfortunate, but you can always stick with the documented, and therefore arguably right, attribute name, nodeValue. Yes, it's verbose, but then so is all of minidom, as well as slower than the excellent xml.etree.ElementTree (esp. in the latter's C implementation, xml.etree.cElementTree), so presumably if you choose to use minidom it must be because you like extensive verbosity...;-).

Alex Martelli
Thanks. ElementTree is too unlike the XML DOM processing I'm used to in other languages, hence went with minidom. The help(xml.dom.minidom.Attr) page did not list nodeValue in the "inherited from" Node or xml.dom.Node sections, even though it is clearly being inherited. Is there a way to inspect (as in reflection) Python classes and types, without relying on 'help' documentation pages that choose to selectively omit key (public!) properties or methods?
rudy
@rudy, `inspect.getmembers(atr)` will show you all the 65 attributes and methods (as pairs `(name, value)`) of object `atr`. You can be more selective, too, of course.
Alex Martelli
Grrreat! Just what I needed. Thanks Alex!
rudy
The value property of attributes is documented in the W3C DOM spec at http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-221662474
Amos Newcombe