python写xml文件的操作实例


Posted in Python onOctober 05, 2014

本文实例讲述了python写xml文件的操作的方法,分享给大家供大家参考。具体方法如下:

要生成的xml文件格式如下:

<?xml version="1.0" ?> 
<!--Simple xml document__chapter 8--> 
<book> 
  <title> 
    sample xml thing 
  </title> 
  <author> 
    <name> 
      <first> 
        ma 
      </first> 
      <last> 
        xiaoju 
      </last> 
    </name> 
    <affiliation> 
      Springs Widgets, Inc. 
    </affiliation> 
  </author> 
  <chapter number="1"> 
    <title> 
      First 
    </title> 
    <para> 
      I think widgets are greate.You should buy lots of them forom 
      <company> 
        Spirngy Widgts, Inc 
      </company> 
    </para> 
  </chapter> 
</book>

Python实现代码如下:

from xml.dom import minidom, Node 
 
doc = minidom.Document() 
 
doc.appendChild(doc.createComment("Simple xml document__chapter 8")) 
 
#generate the book 
book = doc.createElement('book') 
doc.appendChild(book) 
 
#the title 
title = doc.createElement('title') 
title.appendChild(doc.createTextNode("sample xml thing")) 
book.appendChild(title) 
 
#the author section 
author = doc.createElement("author") 
book.appendChild(author) 
name = doc.createElement('name') 
author.appendChild(name) 
firstname = doc.createElement('first') 
firstname.appendChild(doc.createTextNode("ma")) 
name.appendChild(firstname) 
lastname = doc.createElement('last') 
name.appendChild(lastname) 
lastname.appendChild(doc.createTextNode("xiaoju")) 
 
affiliation = doc.createElement("affiliation") 
affiliation.appendChild(doc.createTextNode("Springs Widgets, Inc.")) 
author.appendChild(affiliation) 
 
#The chapter 
chapter = doc.createElement('chapter') 
chapter.setAttribute('number', '1') 
title = doc.createElement('title') 
title.appendChild(doc.createTextNode("First")) 
chapter.appendChild(title) 
book.appendChild(chapter) 
 
para = doc.createElement('para') 
para.appendChild(doc.createTextNode("I think widgets are greate.\ 
You should buy lots of them forom")) 
company = doc.createElement('company') 
company.appendChild(doc.createTextNode("Spirngy Widgts, Inc")) 
para.appendChild(company) 
chapter.appendChild(para) 
 
print doc.toprettyxml()

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
python定时采集摄像头图像上传ftp服务器功能实现
Dec 23 Python
Python开发WebService系列教程之REST,web.py,eurasia,Django
Jun 30 Python
编写Python脚本来获取Google搜索结果的示例
May 04 Python
python spyder中读取txt为图片的方法
Apr 27 Python
Python工厂函数用法实例分析
May 14 Python
在PYQT5中QscrollArea(滚动条)的使用方法
Jun 14 Python
python Django的web开发实例(入门)
Jul 31 Python
tensorflow -gpu安装方法(不用自己装cuda,cdnn)
Jan 20 Python
安装Anaconda3及使用Jupyter的方法
Oct 27 Python
python爬虫泛滥的解决方法详解
Nov 25 Python
Python爬虫逆向分析某云音乐加密参数的实例分析
Dec 04 Python
一起来学习Python的元组和列表
Mar 13 Python
python实现上传样本到virustotal并查询扫描信息的方法
Oct 05 #Python
python实现计算资源图标crc值的方法
Oct 05 #Python
python求crc32值的方法
Oct 05 #Python
Python获取文件ssdeep值的方法
Oct 05 #Python
python获取Linux下文件版本信息、公司名和产品名的方法
Oct 05 #Python
python获取文件版本信息、公司名和产品名的方法
Oct 05 #Python
python实现调用其他python脚本的方法
Oct 05 #Python
You might like
eaglephp使用微信api接口开发微信框架
2014/01/09 PHP
PHP中的日期加减方法示例
2014/08/21 PHP
php中in_array函数用法探究
2014/11/25 PHP
完美解决php 导出excle的.csv格式的数据时乱码问题
2017/02/18 PHP
jquery关于页面焦点的定位(文本框获取焦点时改变样式 )
2010/09/10 Javascript
js下拉框二级关联菜单效果代码具体实现
2013/08/03 Javascript
Javascript浅谈之引用类型
2013/12/18 Javascript
javascript框架设计读书笔记之模块加载系统
2014/12/02 Javascript
JavaScript解析json格式数据简单示例
2014/12/09 Javascript
js+canvas绘制矩形的方法
2016/01/28 Javascript
浅谈JavaScript 覆盖原型以及更改原型
2016/08/31 Javascript
使用JS中的Replace()方法遇到的问题小结
2017/10/20 Javascript
在angular 6中使用 less 的实例代码
2018/05/13 Javascript
[53:03]Optic vs TNC 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
[01:08:00]Fnatic vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
[03:35]2018年度DOTA2最佳辅助位选手5号位-完美盛典
2018/12/17 DOTA
跟老齐学Python之类的细节
2014/10/13 Python
Python动态生成多维数组的方法示例
2018/08/09 Python
元组列表字典(莫烦python基础)
2019/04/03 Python
解决python明明pip安装成功却找不到包的问题
2019/08/28 Python
python turtle 绘制太极图的实例
2019/12/18 Python
Python使用tkinter实现小时钟效果
2021/02/22 Python
小程序瀑布流解决左右两边高度差距过大的问题
2019/02/20 HTML / CSS
Baracuta官方网站:Harrington夹克,G9,G4,G10等
2018/03/06 全球购物
Bibloo荷兰:女士、男士和儿童的服装、鞋子和配饰
2019/02/25 全球购物
新加坡领先的在线生活方式和杂货购物网站:EAMART
2019/04/02 全球购物
全球工业:Global Industrial
2020/02/01 全球购物
俄罗斯小米家用电器、电子产品和智能家居商店:Poood.ru
2020/04/03 全球购物
Java如何格式化日期
2012/08/07 面试题
什么叫应用程序域?什么是托管代码?什么是强类型系统?什么是装箱和拆箱?什么是重载?CTS、CLS和CLR分别作何解释?
2012/05/23 面试题
2014年酒店工作总结范文
2014/11/17 职场文书
介绍信范文大全
2015/05/07 职场文书
2015年暑期社会实践总结
2015/07/13 职场文书
新郎新娘致辞
2015/07/31 职场文书
《黄道婆》教学反思
2016/02/22 职场文书
简短的36句中秋节祝福信息语句
2019/09/09 职场文书