python创建xml_python创建xml

参考了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:基本照这个例子就可以学会怎么创建节点和生成数据,关于其中变量的操作还有别的函数

-

myfolderPath

0001.png

-

the Synapse DataBase

synapseEM

0

-

I do not know

0

-

synapse

Unspecified

0

0

-

157

157

157

157

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值