且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Python ElementTree查找包含特定文本的“标题” xml标记的父元素

更新时间:2023-11-07 13:44:04

执行(嵌套)迭代需要2个步骤: sec ,然后点击标题。像这样的东西:

Do a (nested) iteration in 2 steps: on sec and then on title. Something like:

for sec in parent.iter("sec"):
    for title in sec.iter("title"):
        text = title.text
        if text and "methods" in text.lower():
            print("**title: " + text + " **** sec id: " + sec.get("id", ""))

有关更多详细信息,请检查 [Python 3.Docs]:xml.etree.ElementTree-ElementTree XML API

For more details, check [Python 3.Docs]: xml.etree.ElementTree - The ElementTree XML API.