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的string对象没有contains方法,不用使用string.contain...

python实践项目之监控当前联网状态详情

python实践项目之监控当前联网状态详情

介绍一个利用Python监控当前联网状态情况的python代码,它可以清楚地知道,你的电脑网络是否是链接成功或失败,通俗的说,就是查看你的电脑有木有网,代码如下: 调用系统网络诊断 监...

django 实现celery动态设置周期任务执行时间

django 实现celery动态设置周期任务执行时间

蓝鲸paas平台app开发经验分享 腾讯蓝鲸智云是一套基于PaaS的技术解决方案,提供了完善的前后台开发框架、调度引擎、公共组件等模块,帮助业务的产品和技术人员快速构建低成本、免运维的支...

python中的二维列表实例详解

1. 使用输入值初始化列表 nums = [] rows = eval(input("请输入行数:")) columns = eval(input("请输入列数:")) for ro...

Python中with及contextlib的用法详解

Python中with及contextlib的用法详解

本文实例讲述了Python中with及contextlib的用法。分享给大家供大家参考,具体如下: 平常Coding过程中,经常使用到的with场景是(打开文件进行文件处理,然后隐式地执...