如何从xml中提取属性值?

时间:2016-11-04 16:30:16

标签: python xml elementtree celementtree

如果我有以下xml:

<?xml version="1.0" encoding="UTF-8"?>
<dat>
<concept1 id="001" code="246545002"/>
<concept1 id="002" code="938374749"/>
</dat>

我可以通过

找到所有concept1分支
import xml.etree.cElementTree as ET

tree = ET.ElementTree(file='negation.xmi')

root = tree.getroot()

for elem in tree.iterfind('concept1'):
        print elem.tag, elem.attrib

concept1 {'id': '001', 'code': '246545002'}
concept2 {'id': '002', 'code': '938374749'}

如何访问属性值?即查看code的{​​{1}}是否等于concept1的{​​{1}}?

0 个答案:

没有答案