这是我的代码段: -
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
这会产生这样的输出,似乎没问题: -
<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>
它显示元素的地址,我想改为显示值本身,然后将这些值作为字符串元素存储在列表中。
任何帮助?
答案 0 :(得分:2)
函数 getElementsByTagName 返回DOM Element类型的对象,这就是你得到这个结果的原因。即如果您想要访问该元素的属性调用 id 的值,您可以尝试这样做:
field.attributes['id'].value