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斗牛游戏的概率

过年回家,都会约上亲朋好友聚聚会,会上经常会打麻将,斗地主,斗牛。在这些游戏中,斗牛是最受欢迎的,因为可以很多人一起玩,而且没有技术含量,都是看运气(专业术语是概率)。 斗牛的玩法是:...

centos 安装python3.6环境并配置虚拟环境的详细教程

python3.6下载地址: https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz linux 下python 环境配置 统一...

python多线程扫描端口示例

复制代码 代码如下:# -*- coding: cp936 -*-import socketfrom threading import Thread,activeCount,Lockfr...

Python常见字符串操作函数小结【split()、join()、strip()】

本文实例讲述了Python常见字符串操作函数。分享给大家供大家参考,具体如下: str.split(' ') 1.按某一个字符分割,如‘.' >>> s = ('w...

numpy中实现二维数组按照某列、某行排序的方法

如何根据二维数组中的某一行或者某一列排序?假设data是一个numpy.array类型的二维数组,可以利用numpy中的argsort函数进行实现,代码实例如下: data = da...