[英]How to get xpath to an element with xml.etree
在處理XML樹中的特定元素時,我需要獲取此元素的xpath。 例如/ A / B [4] / C / D [1] / Age如果使用lxml,這非常簡單:只需使用tree.getelementpath(child)
但是出於某種原因,我需要使用xml.etree。 在那里怎么辦?
像這樣:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.