简体   繁体   English

如何在python中使用minidom获取元素值并将其存储到列表中?

[英]How to get element value using minidom in python and store them into a list?

Here is my code snippet:- 这是我的代码段:

from xml.dom import minidom
from xml.dom.minidom import parse
xmldoc = minidom.parse('C:\\Users\\folder\\Documents\\Python Training\\XMLFiles\\sample.xml')

dimension = xmldoc.getElementsByTagName("tns:RefDimensionSet")[0]
fields = dimension.getElementsByTagName("tns:Field")

for field in fields:
    print field

This produces this output which seems to be alright:- 这产生了看起来不错的输出:-

<DOM Element: tns:Field at 0x2a46988>
<DOM Element: tns:Field at 0x2a46b08>
<DOM Element: tns:Field at 0x2a46c88>
<DOM Element: tns:Field at 0x2a46e08>
<DOM Element: tns:Field at 0x2a46f88>
<DOM Element: tns:Field at 0x2a47148>
<DOM Element: tns:Field at 0x2a472c8>
<DOM Element: tns:Field at 0x2a47448>
<DOM Element: tns:Field at 0x2a475c8>
<DOM Element: tns:Field at 0x2a47748>

It displays the address of the elements, I want to display the values themselves instead and then store those values in a list as string elements. 它显示了元素的地址,我想改为显示值本身,然后将这些值作为字符串元素存储在列表中。

Any help? 有什么帮助吗?

Function getElementsByTagName returns an object of type DOM Element, this is why you get this result. 函数getElementsByTagName返回一个DOM Element类型的对象,这就是为什么得到此结果的原因。 Ie if you want to access to the value of an attribute call id of that element you can try this: 即,如果您想访问该元素的属性调用ID的值,则可以尝试以下操作:

field.attributes['id'].value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM