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基于ThreadingTCPServer创建多线程代理的方法示例

本文实例讲述了Python基于ThreadingTCPServer创建多线程代理的方法。分享给大家供大家参考,具体如下: #coding=utf8 from BaseHTTPServ...

Python使用matplotlib和pandas实现的画图操作【经典示例】

Python使用matplotlib和pandas实现的画图操作【经典示例】

本文实例讲述了Python使用matplotlib和pandas实现的画图操作。分享给大家供大家参考,具体如下: 画图在工作再所难免,尤其在做数据探索时候,下面总结了一些关于python...

python 计算数据偏差和峰度的方法

numpy.set_printtoptions(edgeitems=5):值过多,显示前5个和后5个 偏度:衡量随机分布的不均衡性,偏度=0,数值相对均匀的分布在两侧 峰度:概率密度在均...

使用py2exe在Windows下将Python程序转为exe文件

前提条件: 需要安装easy-install模块,这是一个python的模块打包工具。 首先下载easy_setup.py的源代码,下载地址: http://pypi.python.o...

使用apiDoc实现python接口文档编写

apiDoc的安装 npm install apidoc -g 点击官方文档 生成api的终端命令:apidoc -i 代码所在路径-o 生成文件的路径 接口文档的编写 文件的简介...