python 在深层节点中查找标签 xml

python find tags in deep nodes xml

我想使用 python ElementTree 库访问 "thirdDepth1" 标记。下面是简化版。在我的真实 xml 中,它的深度是动态的。因此无法通过使用 find()、findall()、iterfind() 给出路径到达它。有什么想法吗?

<root>
    <firstDepth1>
        <secondDepth1>
            <thirdDepth1>thirdDepthVal</thirdDepth1>
        </secondDepth1>
    </firstDepth1>
    <firstDepth2>
        <secondDepth2></secondDepth2>
    </firstDepth2>
</root>
import xml.etree.ElementTree as ET

tree = ET.parse("testxmlreqpython.xml")

root = tree.getroot()

element = root.find(".//thirdDepth1")

print(element)

根据@Luis Muñoz评论,这可以通过使用xpath作为“.//third Depth 1”来实现