python xml.dom.minidom.Attr问题

时间:2022-12-25 22:38:34

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

在Python中使用minidom获取属性,使用“attributes”属性。例如node.attributes [ “ID”]。值

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?

所以如果我有 ,那应该给我“foo”。 node.attributes [“id”]不返回named属性的值,而是返回xml.dom.minidom.Attr实例。但是看看Attr的帮助,通过帮助('xml.dom.minidom.Attr'),没有提到这个神奇的“价值”属性。我喜欢通过查看类型层次结构,实例方法等来学习API。这个“值”属性来自哪里?为什么它没有列在Attr类的页面中?提到的唯一数据描述符是isId,localName和schemaType。它也不是从任何超类继承的。由于我是Python的新手,一些Python大师会启发吗?

2 个解决方案

#1


4  

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.

minidom只是xml.dom接口的一个实现,因此任何专门针对minidom的文档都只是关于xml.dom本身的特性或限制。

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

Attr上的xml.dom文档说,我引用:

Attr inherits from Node, so inherits all its attributes.

Attr继承自Node,因此继承了它的所有属性。

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

Node上的文档实际上以不同的方式命名属性:nodeValue。但是,确实......:

>>> 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...;-).

记录的nodeValue属性具有_un_documented别名值的事实可能被认为是不幸的,但您始终可以坚持使用已记录的,因此可以说是正确的属性名称nodeValue。是的,它很冗长,但是所有minidom都是如此,以及比优秀的xml.etree.ElementTree(特别是后者的C实现,xml.etree.cElementTree)慢,所以假设你选择使用minidom它一定是因为你喜欢广泛的冗长... ;-)。

#2


0  

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.

Geez,之前从未注意过。你不是在开玩笑,在任何地方都没有提到node.value。虽然在xml.dom.minidom中的def __setitem__下,它肯定是在代码中设置的。

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

不知道该说什么,看起来你将不得不使用它。

#1


4  

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.

minidom只是xml.dom接口的一个实现,因此任何专门针对minidom的文档都只是关于xml.dom本身的特性或限制。

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

Attr上的xml.dom文档说,我引用:

Attr inherits from Node, so inherits all its attributes.

Attr继承自Node,因此继承了它的所有属性。

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

Node上的文档实际上以不同的方式命名属性:nodeValue。但是,确实......:

>>> 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...;-).

记录的nodeValue属性具有_un_documented别名值的事实可能被认为是不幸的,但您始终可以坚持使用已记录的,因此可以说是正确的属性名称nodeValue。是的,它很冗长,但是所有minidom都是如此,以及比优秀的xml.etree.ElementTree(特别是后者的C实现,xml.etree.cElementTree)慢,所以假设你选择使用minidom它一定是因为你喜欢广泛的冗长... ;-)。

#2


0  

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.

Geez,之前从未注意过。你不是在开玩笑,在任何地方都没有提到node.value。虽然在xml.dom.minidom中的def __setitem__下,它肯定是在代码中设置的。

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

不知道该说什么,看起来你将不得不使用它。