用如何用Python完成创建生成xml文档文件的工作。
需要用到的方法:from xml.dom.minidom import Document,想必大家很熟悉这个操作了,接下来就用python 创建create 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()
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/code/c2678.html
相关文章 Recommend