简体   繁体   English

python在深节点xml中查找标签

[英]python find tags in deep nodes xml

I want to reach the "thirdDepth1" tag using python ElementTree library. 我想使用python ElementTree库到达“ thirdDepth1”标签。 Below is the simplified version. 以下是简化版本。 In my real xml its depth is dynamic. 在我的真实xml中,它的深度是动态的。 So can't reach it with giving path, using find(),findall(), iterfind(). 因此无法使用find(),findall()和iterfind()给出路径。 Any ideas? 有任何想法吗?

<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)

From @Luis Muñoz comment, this can be achived by using xpath as ".//thirdDepth1" 来自@LuisMuñoz的评论,可以通过将xpath用作“ .//thirdDepth1”来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM