Python Create xml

Source: Internet
Author: User

Refer to the implementation of the http://www.cnblogs.com/wangshide/archive/2011/10/29/2228936.html in a once

#-*-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)

########### writing DOM Object doc to file
f = open (' D:\\labproj\\faster_rcnn\\annotation.xml ', ' W ')
F.write (doc.toprettyxml (indent = "))
F.close ()

The following XML is generated: The basic picture can be used to learn how to create nodes and generate data, and there are other functions for manipulating variables.

<?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>

Python Create xml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.