from xml.dom.minidom import Document # 确保caffe在Python路径: import xml.dom.minidomdoc = Document() #创建DOM文档对象Object = doc.createElement('Objects')doc.appendChild(Object) objec_name='person'
lable=15
xmin=207
ymin=106
xmax=489
ymax=381
score=0.99566object_name = doc.createElement(label_name)#object_name.setAttribute('物体类别',label_name)#设置命名空间
Object.appendChild(object_name)object_label =doc.createElement('label')object_label_text = doc.createTextNode(str(label)) #元素内容写入object_label.appendChild(object_label_text)object_name.appendChild(object_label)object_xmin =doc.createElement('xmin')object_xmin_text = doc.createTextNode(str(xmin)) #元素内容写入object_xmin.appendChild(object_xmin_text)object_name.appendChild(object_xmin)object_ymin =doc.createElement('ymin')object_ymin_text = doc.createTextNode(str(ymin)) #元素内容写入object_ymin.appendChild(object_ymin_text)object_name.appendChild(object_ymin)object_xmax =doc.createElement('xmax')object_xmax_text = doc.createTextNode(str(xmax)) #元素内容写入object_xmax.appendChild(object_xmax_text)object_name.appendChild(object_xmax)object_ymax =doc.createElement('ymax')object_ymax_text = doc.createTextNode(str(ymax)) #元素内容写入object_ymax.appendChild(object_ymax_text)object_name.appendChild(object_ymax)object_score= doc.createElement('score')object_score_text = doc.createTextNode(str(score)) #元素内容写入object_score.appendChild(object_score_text)object_name.appendChild(object_score)f = open('parameter.xml','w')doc.writexml(f,indent = '\t',newl = '\n', addindent = '\t',encoding='utf-8')f.close()
生成的xml文件
<?xml version="1.0" encoding="utf-8"?><Objects><person><label>15</label><xmin>207</xmin><ymin>106</ymin><xmax>489</xmax><ymax>381</ymax><score>0.99566</score></person></Objects