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

相关文章

python中强大的format函数实例详解

python中format函数用于字符串的格式化 自python2.6开始,新增了一种格式化字符串的函数str.format(),此函数可以快速处理各种字符串。 语法 它通过{}和:来代...

python os.listdir按文件存取时间顺序列出目录的实例

如下所示: import os DIR = "/home/serho/workspace/lisp" def compare(x, y): stat_x = os.stat(D...

Python探索之pLSA实现代码

pLSA(probabilistic Latent Semantic Analysis),概率潜在语义分析模型,是1999年Hoffman提出的一个被称为第一个能解决一词多义问题的模型,...

Python调用C语言的实现

Python中的ctypes模块可能是Python调用C方法中最简单的一种。ctypes模块提供了和C语言兼容的数据类型和函数来加载dll文件,因此在调用时不需对源文件做任何的修改。也正...

python 同时运行多个程序的实例

start many programs execfile('C:/Dokumente und Einstellungen/schnei17/Desktop/python/zeit/1...