У меня есть файл 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>
Я хочу получить доступ к тегу <imageName>
после того, как достигну тега <taggedRectangle>
. Я написал следующий код
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
. Почему?
(местоположения. xml относится к имени файла xml на моем устройстве, содержимое которого похоже на файл XML в начале вопроса)
Примечание - https://docs.python.org/3/library/xml.etree.elementtree.html Согласно документации, мой синтаксис мне кажется действительным, но я не могу понять ошибку.