简体   繁体   English

如何使用xml.etree获取元素的xpath

[英]How to get xpath to an element with xml.etree

While working on a specific element in the XML tree, I need to get the xpath of this element. 在处理XML树中的特定元素时,我需要获取此元素的xpath。 For example /A/B[4]/C/D[1]/Age If using lxml, it is very simple: Simply use tree.getelementpath(child) 例如/ A / B [4] / C / D [1] / Age如果使用lxml,这非常简单:只需使用tree.getelementpath(child)

But for a reason I need to use xml.etree. 但是出于某种原因,我需要使用xml.etree。 How can it be done there? 在那里怎么办?

Something like this: 像这样:

import xml.etree.ElementTree as ET

root = ET.fromstring(you_xml_code)
root.findall('./A/B[4]/C/D[1]/Age')

https://docs.python.org/2/library/xml.etree.elementtree.html#example https://docs.python.org/2/library/xml.etree.elementtree.html#example

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

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