Python创建xml的方法

yipeiwu_com5年前Python基础

本文实例讲述了Python创建xml的方法。分享给大家供大家参考。具体实现方法如下:

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)
  def display(self):
    print self.toprettyxml(indent="  ")
wx = write_xml()
wx.set_tag('test')
wx.display()

希望本文所述对大家的Python程序设计有所帮助。

相关文章

pytorch 修改预训练model实例

我就废话不多说了,直接上代码吧! class Net(nn.Module): def __init__(self , model): super(Net, self)._...

Python3.x中自定义比较函数

在Python3.x的世界里,cmp函数没有了。那么sorted,min,max等需要比较函数作为参数的函数该如何用呢? 以min函数的定义为例,有两种重载形式: 单参数(一个迭代器):...

Python中暂存上传图片的方法

很简单的代码,记录一下。 复制代码 代码如下:     import Image     image = Image.open...

python 实现按对象传值

python 实现按对象传值

今天研究了下Python中的传值问题,通常在C、C++中有按值传递和按引用传递两种情况,按值传递时会拷贝实参,而按引用传递时只是给形参赋了一个指向实参的指针,但在python却没有区分这...

Python数据结构与算法之图的广度优先与深度优先搜索算法示例

本文实例讲述了Python数据结构与算法之图的广度优先与深度优先搜索算法。分享给大家供大家参考,具体如下: 根据维基百科的伪代码实现: 广度优先BFS: 使用队列,集合 标记初始结点已被...