首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python2.7 xml.etree。获取同级

Python2.7 xml.etree。获取同级
EN

Stack Overflow用户
提问于 2020-08-24 17:32:38
回答 1查看 25关注 0票数 0

我得到的xml的结构类似于下面的示例

代码语言:javascript
复制
            [...]
            <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通过标识符块的根属性搜索标题栏?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-24 17:51:53

下面

代码语言:javascript
复制
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)

输出

代码语言:javascript
复制
Section title
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63558315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档