Python创建xml的方法

yipeiwu_com6年前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使用百度文字识别功能方法详解

python使用百度文字识别功能方法详解

介绍python使用百度智能去的文字识别功能,可以识别截图中的文,登陆路验证码等等。, 登陆百度智能云,选择产品服务。 选择“人工智能”---文字识别。 点击创建应用。 如图下面有关...

python时间序列按频率生成日期的方法

有时候我们的数据是按某个频率收集的,比如每日、每月、每15分钟,那么我们怎么产生对应频率的索引呢?pandas中的date_range可用于生成指定长度的DatetimeIndex。 我...

Python利用matplotlib绘制约数个数统计图示例

Python利用matplotlib绘制约数个数统计图示例

本文实例讲述了Python利用matplotlib绘制约数个数统计图。分享给大家供大家参考,具体如下: 利用Python计算1000以内自然数的约数个数,然后通过matplotlib绘制...

详解Python正则表达式re模块

正则是处理字符串最常用的方法,我们编码中到处可见正则的身影。 正则大同小异,python 中的正则跟其他语言相比略有差异: 1、替换字符串时,替换的字符串可以是一个函数 2、split...

python中使用序列的方法

本文实例讲述了python中使用序列的方法。分享给大家供大家参考。具体如下: 列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符和切片操作符...