Python 2.7 xml.etree。结亲
我得到的xml的结构类似于下面的示例Python 2.7 xml.etree。结亲,python,xml,python-2.7,xml-parsing,Python,Xml,Python 2.7,Xml Parsing,我得到的xml的结构类似于下面的示例 [...] <a> <list> <section> <identifier root="88844433"></templateId> <code code="6664.2" display=&
[...]
<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