参考了http://www.cnblogs.com/wangshide/archive/2011/10/29/2228936.html中的实现了一遍
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 01 15:37:46 2016
@author: raoqiang
"""
#coding=utf-8
import xml.dom.minidom as minidom
from xml.dom.minidom import Document
doc = Document() #创建DOM文档对象
def create_annotation(doc,file)
Annot = doc.createElement('annotation') #创建根元素
doc.appendChild(Annot)
folder = doc.createElement('folder')
folder_text = doc.createTextNode('myfolderPath') #元素内容写入
folder.appendChild(folder_text)
Annot.appendChild(folder)
fName = doc.createElement('filename')
fName_text = doc.createTextNode('0001.png') #元素内容写入
fName.appendChild(fName_text)
Annot.appendChild(fName)
src = doc.createElement('source')
db = doc.createElement('database')
db_text = doc.createTextNode('the Synapse DataBase');
db.appendChild(db_text)
src.appendChild(db)
annt = doc.createElement('annotation')
annt_text = doc.createTextNode('the Synapse DataBase');
annt.appendChild(annt_text)
src.appendChild(db)
img= doc.createElement('image')
img_text = doc.createTextNode('synapseEM')
img.appendChild(img_text)
src.appendChild(img)
flickrid = doc.createElement('flickrid')
flickrid_text = doc.createTextNode(str(0))
flickrid.appendChild(flickrid_text)
src.appendChild(flickrid)
Annot.appendChild(src)
owner = doc.createElement('owner')
flickrid = doc.createElement('flickrid')
flickrid_text = doc.createTextNode('I do not know')
flickrid.appendChild(flickrid_text)
owner.appendChild(flickrid)
Annot.appendChild(owner)
size = doc.createElement('size')
width = doc.createElement('width')
width_text = doc.createTextNode(str(239))
width.appendChild(width_text)
size.appendChild(width_text)
height = doc.createElement('height')
height_text = doc.createTextNode(str(239))
height.appendChild(height_text)
size.appendChild(height_text)
depth = doc.createElement('depth')
depth_text = doc.createTextNode(str(1))
depth.appendChild(depth_text)
size.appendChild(depth_text)
Annot.appendChild(owner)
seg = doc.createElement('segmented')
seg_text = doc.createTextNode(str(0))
seg.appendChild(seg_text)
Annot.appendChild(seg)
obj = doc.createElement('object')
name = doc.createElement('name')
name_text = doc.createTextNode('synapse');
name.appendChild(name_text)
obj.appendChild(name)
pos = doc.createElement('pose')
pos_text = doc.createTextNode('Unspecified')
pos.appendChild(pos_text)
obj.appendChild(pos)
trun = doc.createElement('truncated')
trun_text = doc.createTextNode(str(0))
trun.appendChild(trun_text)
obj.appendChild(trun)
diffi = doc.createElement('difficult')
diffi_text = doc.createTextNode(str(0))
diffi.appendChild(diffi_text)
obj.appendChild(diffi)
bndbox = doc.createElement('bndbox')
xmin = doc.createElement('xmin')
xmin_text = doc.createTextNode(str(157))
xmin.appendChild(xmin_text)
bndbox.appendChild(xmin)
xmax = doc.createElement('xmax')
xmax_text = doc.createTextNode(str(157))
xmax.appendChild(xmax_text)
bndbox.appendChild(xmax)
ymin = doc.createElement('ymin')
ymin_text = doc.createTextNode(str(157))
ymin.appendChild(ymin_text)
bndbox.appendChild(ymin)
ymax = doc.createElement('ymax')
ymax_text = doc.createTextNode(str(157))
ymax.appendChild(ymax_text)
bndbox.appendChild(ymax)
obj.appendChild(bndbox)
Annot.appendChild(obj)
########### 将DOM对象doc写入文件
f = open('D:\LabProj\faster_rcnn\annotation.xml','w')
f.write(doc.toprettyxml(indent = ''))
f.close()
生成了如下的xml:基本照这个例子就可以学会怎么创建节点和生成数据,关于其中变量的操作还有别的函数
<?xml version="1.0"?>
-<annotation>
<folder>myfolderPath</folder>
<filename>0001.png</filename>
-<source>
<database>the Synapse DataBase</database>
<image>synapseEM</image>
<flickrid>0</flickrid>
</source>
-<owner>
<flickrid>I do not know</flickrid>
</owner>
<segmented>0</segmented>
-<object>
<name>synapse</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
-<bndbox>
<xmin>157</xmin>
<xmax>157</xmax>
<ymin>157</ymin>
<ymax>157</ymax>
</bndbox>
</object>
</annotation>