使用 ElementTree 在 XML 文件中向上导航
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)不太可能得到
父元素。
打开 The ElementTree XML API 站点(在您的 post 中提到)并找到
支持的 XPath 语法。 .. 的描述如下:
Returns 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.
我有一个 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)不太可能得到
父元素。
打开 The ElementTree XML API 站点(在您的 post 中提到)并找到
支持的 XPath 语法。 .. 的描述如下:
Returns 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.