python使用minidom生成XML文件
2013-01-15 16:10
323 查看
找了蛮多,这篇文章最简单实用
1.使用minidom创建XML文件
2.生成的XML文件
1.使用minidom创建XML文件
# -*- coding: cp936 -*- """ 使用minidom生成XML 1.创建Element,createElement 2.添加子节点,appendChild 3.创建Text,createTextNode 4.创建属性,createAttribute """ from xml.dom import minidom,Node # 创建Document doc = minidom.Document() # 创建book节点 book = doc.createElement("book") doc.appendChild(book) # 创建Title节点 title = doc.createElement("title") text = doc.createTextNode("Sample XML Thing") title.appendChild(text) book.appendChild(title) # 创建author节点 author = doc.createElement("author") # 创建name节点 name = doc.createElement("name") first = doc.createElement("first") first.appendChild(doc.createTextNode("Benjamin")) name.appendChild(first) last = doc.createElement("last") last.appendChild(doc.createTextNode("Smith")) name.appendChild(last) author.appendChild(name) book.appendChild(author) # author节点完毕 # 创建chapter节点 chapter = doc.createElement("chapter") chapter.setAttribute("number","1") title = doc.createElement("title") title.appendChild(doc.createTextNode("Fisrt Chapter")) chapter.appendChild(title) para = doc.createElement("para") para.appendChild(doc.createTextNode("I think widgets are great.you should buy lots \ of them from")) company = doc.createElement("company") company.appendChild(doc.createTextNode("Springy widgets,Inc")) para.appendChild(company) chapter.appendChild(para) # chapter节点完毕 book.appendChild(chapter) # book节点完毕 print doc.toprettyxml(indent = " ") |
<?xml version="1.0" ?> <book> <title> Sample XML Thing </title> <author> <name> <first> Benjamin </first> <last> Smith </last> </name> </author> <chapter number="1"> <title> Fisrt Chapter </title> <para> I think widgets are great.you should buy lots of them from <company> Springy widgets,Inc </company>
相关文章推荐
- Python网络编程基础笔记-使用minidom生成XML文件
- Python3.5环境下使用generateDS解析xml文件生成C++头文件的方法
- 使用python的minidom模块格式化、对齐、换行、整理xml文件
- Python中使用dom模块生成XML文件示例
- 使用pyinstaller工具打包python项目代码生成exe文件
- py2exe使用方法详解 Python生成exe步骤
- 使用Python生成随机简单的验证码
- android(15)_使用Pull解析器生成XML文件
- python使用xmlrunner模块生成报告举例
- android使用pull解析器来解析和生成xml文件
- android:shape的使用 (android用xml文件生成图像控件)
- 使用python生成杨辉三角形的示例代码
- [python]利用Python生成xml文件
- android基础--使用Pull解析器生成XML文件
- Python使用PIL模块生成随机验证码
- Python使用UUID库生成唯一ID
- 使用minidom来处理XML的示例(Python 学习)(转载)
- 使用python的os.fork()为一个主进程生成多个子进程
- 使用python创建xml文件
- 使用Pull解析器生成XML文件和读取xml文件