如何用Python创建生成xml文档文件的方法,pythonxml,用Python自动创建一


Python自动创建一个xml文档文件要怎么操作?本文就为大家来参照代码来详解一下如何用Python完成创建生成xml文档文件的工作
需要用到的方法:from xml.dom.minidom import Document,想必大家很熟悉这个操作了,接下来就用python 创建create xml。

python创建xml方法

python目录里面创建xml包,好了下面就开始我们正式的创建xml文档文件的python代码部分了:

from xml.dom.minidom import Document#导入所需方法class write_xml(Document):    def __init__(self):        Document.__init__(self)    def set_tag(self,tag):        self.tag = tag        self.tag1 = self.createElement(self.tag)        self.appendChild(self.tag1)        self.maincard = self.createElement("card")        self.maincard.setAttribute("id", "main")        self.maincard.setAttribute("id2","main2")        self.tag1.appendChild(self.maincard)        self.paragraph1 = self.createElement("p")        self.maincard.appendChild(self.paragraph1)        self.ptext = self.createTextNode("This is a test!")        self.paragraph1.appendChild(self.ptext)#www.iplaypy.com        def display(self):        print self.toprettyxml(indent="   ")wx = write_xml()wx.set_tag('test')wx.display()

推荐阅读相关内容
Python open()函数文件打开、读、写write操作详解

编橙之家文章,

评论关闭