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程序设计有所帮助。

相关文章

详解安装mitmproxy以及遇到的坑和简单用法

详解安装mitmproxy以及遇到的坑和简单用法

mitmproxy 是一款工具,也可以说是 python 的一个包,在命令行操作的工具。 MITM 即中间人攻击(Man-in-the-middle attack) 使用这个工具可以在...

Python中dictionary items()系列函数的用法实例

本文实例讲述了Python中dictionary items()系列函数的用法,对Python程序设计有很好的参考借鉴价值。具体分析如下: 先来看一个示例: import html...

python实现八大排序算法(2)

python实现八大排序算法(2)

本文接上一篇博客python实现的八大排序算法part1,将继续使用python实现八大排序算法中的剩余四个:快速排序、堆排序、归并排序、基数排序 5、快速排序 快速排序是通常被认为在同...

django模型中的字段和model名显示为中文小技巧分享

简单方法: models.py 复制代码 代码如下: class IceCreamBar(models.Model):     title =  ...

Python有参函数使用代码实例

这篇文章主要介绍了Python有参函数使用代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.给定验证码长度n,生成随机验证码...