1、XML编辑器
XmlNoepad下载地址:https://microsoft.github.io/XmlNotepad/#install/
2、两种读取方法
2007_000032.xml文件:
<annotation>
<folder>VOC2012</folder>
<filename>2007_000032.jpg</filename>
<source>
<database>The VOC2007 Database</database>
<annotation>PASCAL VOC2007</annotation>
<image>flickr</image>
</source>
<size>
<width>500</width>
<height>281</height>
<depth>3</depth>
</size>
<segmented>1</segmented>
<object>
<name>aeroplane</name>
<pose>Frontal</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>104</xmin>
<ymin>78</ymin>
<xmax>375</xmax>
<ymax>183</ymax>
</bndbox>
</object>
<object>
<name>aeroplane</name>
<pose>Left</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>133</xmin>
<ymin>88</ymin>
<xmax>197</xmax>
<ymax>123</ymax>
</bndbox>
</object>
<object>
<name>person</name>
<pose>Rear</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>195</xmin>
<ymin>180</ymin>
<xmax>213</xmax>
<ymax>229</ymax>
</bndbox>
</object>
<object>
<name>person</name>
<pose>Rear</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>26</xmin>
<ymin>189</ymin>
<xmax>44</xmax>
<ymax>238</ymax>
</bndbox>
</object>
</annotation>
2.1 读取某个节点的值
读取脚本:
import xml.etree.ElementTree as ET
tree = ET.parse('2007_000032.xml')
w = tree.find('size').find('width').text
h = tree.find('size').find('height').text
print(w)
print(h)
读取效果:
2.2 遍历读取某种节点的值
读取脚本:
import xml.etree.ElementTree as ET
tree = ET.parse('2007_000032.xml')
objects = []
for obj in tree.findall('object'):
obj_dict = dict()
obj_dict['name'] = obj.find('name').text
obj_dict['pose'] = obj.find('pose').text
objects.append(obj_dict)
print(len(objects),objects)
print(objects[3]['name'])
读取效果:
参考:
https://blog.youkuaiyun.com/weixin_41896770/article/details/128426869