问题描述
我得到的xml具有与以下示例相似的结构
[...]
<a>
<list>
<section>
<identifier root="88844433"></templateId>
<code code="6664.2" display="Relevant"></code>
<title>Section title </title>
</section>
</a>
</list>
[...]
如何在Python2.7中使用xml.etree通过标识符块的根属性搜索标题块?
解决方法
下方
import xml.etree.ElementTree as ET
xml = ''' <a>
<list>
<section>
<templateId root="12"></templateId>
<code code="6664.2" display="Relevant"></code>
<title>Section title </title>
</section>
</list>
</a>'''
root = ET.fromstring(xml)
section = root.find(".//section/templateId[@root='12']/..")
print(section.find('title').text)
输出
Section title