[英]Navigating Upwards in XML file using ElementTree
我有一個看起來像這樣的 XML 文件
<tagset>
<image>
<imageName>apanar_06.08.2002/IMG_1261.JPG</imageName>
<resolution x="1600" y="1200" />
<taggedRectangles>
<taggedRectangle x="174.0" y="392.0" width="274.0" height="195.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="512.0" y="391.0" width="679.0" height="183.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="184.0" y="612.0" width="622.0" height="174.0" offset="-2.0" rotation="0.0" userName="admin" />
<taggedRectangle x="863.0" y="599.0" width="446.0" height="187.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="72.0" y="6.0" width="95.0" height="87.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="247.0" y="2.0" width="197.0" height="88.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="792.0" y="0.0" width="115.0" height="81.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="200.0" y="848.0" width="228.0" height="139.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="473.0" y="878.0" width="165.0" height="109.0" offset="14.0" rotation="0.0" userName="admin" />
<taggedRectangle x="684.0" y="878.0" width="71.0" height="106.0" offset="12.0" rotation="0.0" userName="admin" />
<taggedRectangle x="806.0" y="844.0" width="218.0" height="141.0" offset="26.0" rotation="0.0" userName="admin" />
</taggedRectangles>
</image>
</tagset>
我想在到達<taggedRectangle>
標記后訪問<imageName>
標記。 我寫了以下代碼
import xml.etree.ElementTree as ET
with open('locations.xml','r') as file:
contents = file.read()
tree = ET.fromstring(contents) #tree represents the root node of the xml file which is tagset
tags = tree.findall(".//taggedRectangle")
attribs = ['x','y','width','height']
x = []
y = []
width = []
height = []
imageName = []
for tag in tags:
x.append(tag.get('x'))
y.append(tag.get('y'))
width.append(tag.get('width'))
height.append(tag.get('height'))
print(type(tag))
print(tag.tag)
temp = tag.find('./..') ####THIS LINE IS BEING REFERRED AFTERWARDS####
print(type(temp))
現在,在我上面突出顯示的行中,我希望temp
指的是<taggedRectangles
> 節點。 但它顯示我輸入NoneType
。 為什么?
(locations.xml是指我設備中的xml文件的名稱,其內容類似於問題開頭的XML文件)
注意 - https://docs.python.org/3/library/xml.etree.elementtree.html根據文檔,我的語法對我來說似乎是有效的,但我無法弄清楚錯誤。
請注意, temp = tag.find('./..')
(使用ElementTree )不太可能獲得父元素。 打開ElementTree XML API站點(在您的帖子中提到)並找到Supported XPath syntax 。 ..的描述為:如果路徑試圖到達起始元素的祖先,則返回 None 。
我認為解決問題的正確方法是使用lxml而不是ElementTree ,因為它包含ElementTree中缺少的一些方法和功能。
我嘗試了以下代碼:
from lxml import etree as et
root = et.XML(contents) # contents contains your XML
for elem in root.findall('.//taggedRectangle'):
x = elem.get('x')
pic = elem.xpath('../../imageName')[0].text
print(x, pic)
並得到以下結果:
174.0 apanar_06.08.2002/IMG_1261.JPG
512.0 apanar_06.08.2002/IMG_1261.JPG
184.0 apanar_06.08.2002/IMG_1261.JPG
863.0 apanar_06.08.2002/IMG_1261.JPG
72.0 apanar_06.08.2002/IMG_1261.JPG
247.0 apanar_06.08.2002/IMG_1261.JPG
792.0 apanar_06.08.2002/IMG_1261.JPG
200.0 apanar_06.08.2002/IMG_1261.JPG
473.0 apanar_06.08.2002/IMG_1261.JPG
684.0 apanar_06.08.2002/IMG_1261.JPG
806.0 apanar_06.08.2002/IMG_1261.JPG
如您所見,例如在xpath方法中,您可以在XPath表達式中自由使用雙點,不受ElementTree的限制。 但是這種方法只存在於lxml中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.