1. 程式人生 > >python xml讀寫

python xml讀寫

1. xml例子
<?xml version="1.0" encoding="UTF-8"?>
<annotation>
  <folder>VOC2007</folder>
  <filename>01240001.jpg</filename>
  </object>
  <object>
    <name>like</name>
    <pose>Unspecified</pose>
    <bndbox>
      <xmin>211</xmin>
      <ymin>152</ymin>
      <xmax>239</xmax>
      <ymax>194</ymax>
    </bndbox>
  </object>
</annotation>
2.xml讀

    舉個讀box中xmin、xmax、ymin、ymax以及類名的例子:

import xml.dom.minidom
dom = xml.dom.minidom.parse( xml_path ) #用於開啟一個xml檔案,並將這個檔案物件dom變數。                                                
root = dom.documentElement #用於得到dom物件的文件元素,並把獲得的物件給root
    
name_lst = []
lst_temp = root.getElementsByTagName("name")
for i in range(len(lst_temp)):
    l = lst_temp[i].firstChild.data
    if l in class_name:
       name_lst.append(l)    
                    
xmin_lst1 = root.getElementsByTagName("xmin")
ymin_lst1 = root.getElementsByTagName("ymin")
xmax_lst1 = root.getElementsByTagName("xmax")
ymax_lst1 = root.getElementsByTagName("ymax")
num = min(len(xmin_lst1), len(name_lst))
xmin_lst = []
xmax_lst = []
ymin_lst = []
ymax_lst = []
for i in range(num):
     xmin = int(xmin_lst1[i].firstChild.data)
     xmax = int(xmax_lst1[i].firstChild.data)
     ymin = int(ymin_lst1[i].firstChild.data)
     ymax = int(ymax_lst1[i].firstChild.data)
    
     #print type(xmin_lst1[i].firstChild.data) #<type 'unicode'>
 

3. 寫xml
   # 建立dom文件
    doc = Document()
    # 建立根節點
    orderlist = doc.createElement('annotation')
    # 根節點插入dom樹
    doc.appendChild(orderlist)
    
    # 每一組資訊先建立節點<folder>,然後插入到父節點<orderlist>下
    folder = doc.createElement('folder')
    orderlist.appendChild(folder)
    # 建立<folder>下的文字節點
    folder_text = doc.createTextNode("VOC2007")
    # 將文字節點插入到<folder>下
    folder.appendChild(folder_text)
    
    filename = doc.createElement('filename')
    orderlist.appendChild(filename)
    filename_text = doc.createTextNode(file)
    filename.appendChild(filename_text)
    for i in range(num):
	object = doc.createElement('object')
	orderlist.appendChild(object)
	name = doc.createElement('name')
	object.appendChild(name)
	name_text = doc.createTextNode(name_lst[i])
	name.appendChild(name_text)
		
	pose = doc.createElement('pose')
	object.appendChild(pose)
	pose_text = doc.createTextNode("Unspecified")
	pose.appendChild(pose_text)
				
        #########
	bndbox = doc.createElement('bndbox')
	object.appendChild(bndbox)
	xmin = doc.createElement('xmin')
	bndbox.appendChild(xmin)
	xmin_text = doc.createTextNode(str(xmin_lst[i]))
	xmin.appendChild(xmin_text)
		
	ymin = doc.createElement('ymin')
	bndbox.appendChild(ymin)
	ymin_text = doc.createTextNode(str(ymin_lst[i]))
	ymin.appendChild(ymin_text)
		
	xmax = doc.createElement('xmax')
	bndbox.appendChild(xmax)
	xmax_text = doc.createTextNode(str(xmax_lst[i]))
	xmax.appendChild(xmax_text)
		
	ymax = doc.createElement('ymax')
	bndbox.appendChild(ymax)
	ymax_text = doc.createTextNode(str(ymax_lst[i]))
	ymax.appendChild(ymax_text)
		
with open(save_xml_dir + "/" + file[:len(file)-4] + ".xml", 'w') as f:
    f.write(doc.toprettyxml(indent='\t', encoding='utf-8'))