Python 2.7 xml.etree。得到兄弟姐妹
Python 2.7 xml.etree. Getting sibling
我得到了 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
我得到了 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