【发布时间】:2017-05-31 11:00:41
【问题描述】:
XML 文件:
<?xml version="1.0" encoding="iso-8859-1"?>
<rdf:RDF xmlns:cim="http://iec.ch/TC57/2008/CIM-schema-cim13#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<cim:Terminal rdf:ID="A_T1">
<cim:Terminal.ConductingEquipment rdf:resource="#A_EF2"/>
<cim:Terminal.ConnectivityNode rdf:resource="#A_CN1"/>
</cim:Terminal>
</rdf:RDF>
我想获取 Terminal.ConnnectivityNode 元素的属性值和 Terminal 元素的属性值也作为上述 xml 的输出。我已经尝试过以下方式!
Python 代码:
from elementtree import ElementTree as etree
tree= etree.parse(r'N:\myinternwork\files xml of bus systems\cimxmleg.xml')
cim= "{http://iec.ch/TC57/2008/CIM-schema-cim13#}"
rdf= "{http://www.w3.org/1999/02/22-rdf-syntax-ns#}"
将下面一行添加到代码中
print tree.find('{0}Terminal'.format(cim)).attrib
输出1::符合预期
{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}ID': 'A_T1'}
如果我们将下面这一行附加到上面的代码中
print tree.find('{0}Terminal'.format(cim)).attrib['rdf:ID']
output2:rdf:ID 中的键错误
如果我们将下面这一行附加到上面的代码中
print tree.find('{0}Terminal/{0}Terminal.ConductivityEquipment'.format(cim))
输出3无
如何获得 output2 作为 A_T1 和 Output3 作为 #A_CN1?
上面代码中的{0}是什么意思,我发现必须通过net来使用没明白它的意义?
【问题讨论】:
-
Nitpick:
#A_T1在 XML 文档中找不到。只有A_T1 -
非常感谢您的编辑
标签: python xml python-2.7 xml-namespaces elementtree