如何用Python创建生成xml文档文件的方法,pythonxml,用Python自动创建一
如何用Python创建生成xml文档文件的方法,pythonxml,用Python自动创建一
用Python自动创建一个xml文档文件要怎么操作?本文就为大家来参照代码来详解一下如何用Python完成创建生成xml文档文件的工作。
需要用到的方法:from xml.dom.minidom import Document,想必大家很熟悉这个操作了,接下来就用python 创建create xml。
python目录里面创建xml包,好了下面就开始我们正式的创建xml文档文件的python代码部分了:
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)#www.iplaypy.com def display(self): print self.toprettyxml(indent=" ")wx = write_xml()wx.set_tag('test')wx.display()
推荐阅读相关内容:
Python open()函数文件打开、读、写write操作详解
编橙之家文章,
相关内容
- 用Python解压缩rar、zip文件的方法,python解压缩rarzip,编橙
- 游戏水桶倒水问题Python语言的解决方法,倒水python,Py
- Python算法--最长公共子串算法代码讲解,python算法,Pyt
- 如何用Python os.path.walk方法遍历搜索文件内容的操作详解
- Python脚本随机生成中文验证码源码实例分析,python实例
- Python标准库模块之Sys使用详解,python使用详解,本文主要
- 本地服务更新Python代码 如要使用请适当的修改,pytho
- python抽奖 系统算法代码的简单实现,python抽奖算法代码
- 如何用Python代码实现自动比较两个文件中的代码变化?
- python logging 日志模块使用方法学习,pythonlogging,本文为
评论关闭