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实现绘制树枝简单示例
Jul 24 Python
python+mysql实现简单的web程序
Sep 11 Python
使用Python脚本操作MongoDB的教程
Apr 16 Python
python过滤字符串中不属于指定集合中字符的类实例
Jun 30 Python
Python中字符串的格式化方法小结
May 03 Python
在python3环境下的Django中使用MySQL数据库的实例
Aug 29 Python
python中验证码连通域分割的方法详解
Jun 04 Python
python游戏开发之视频转彩色字符动画
Apr 26 Python
使用Python制作新型冠状病毒实时疫情图
Jan 28 Python
Python tkinter布局与按钮间距设置方式
Mar 04 Python
[原创]赚疯了!转手立赚800+?大佬的python「抢茅台脚本」使用教程
Jan 12 Python
Python Django框架介绍之模板标签及模板的继承
May 27 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
PHP对文件进行加锁、解锁实例
2015/01/23 PHP
php递归遍历多维数组的方法
2015/04/18 PHP
php的crc32函数使用时需要注意的问题(不然就是坑)
2015/04/21 PHP
Thinkphp和onethink实现微信支付插件
2016/04/13 PHP
静态html文件执行php语句的方法(推荐)
2016/11/21 PHP
PHP Class SoapClient not found解决方法
2018/01/20 PHP
javascript FormatNumber函数实现方法
2008/12/30 Javascript
JS实现点击链接取消跳转效果的方法
2014/01/24 Javascript
IE下通过a实现location.href 获取referer的值
2014/09/04 Javascript
原生JS实现LOADING效果
2015/03/16 Javascript
js实现图片缓慢放大缩小效果
2016/08/02 Javascript
Javascript实现数组中的元素上下移动
2017/04/28 Javascript
nodejs超出最大的调用栈错误问题
2017/12/27 NodeJs
NodeJS 将文件夹按照存放路径变成一个对应的JSON的方法
2018/10/17 NodeJs
vue+Element-ui实现分页效果实例代码详解
2018/12/10 Javascript
extract-text-webpack-plugin用法详解
2019/02/14 Javascript
详解超简单的react服务器渲染(ssr)入坑指南
2019/02/28 Javascript
Python记录详细调用堆栈日志的方法
2015/05/05 Python
Python实现栈的方法
2015/05/26 Python
用ReactJS和Python的Flask框架编写留言板的代码示例
2015/12/19 Python
Python3 socket同步通信简单示例
2017/06/07 Python
浅谈Python 的枚举 Enum
2017/06/12 Python
python操作MySQL 模拟简单银行转账操作
2017/09/27 Python
pandas 快速处理 date_time 日期格式方法
2018/11/12 Python
Python selenium爬虫实现定时任务过程解析
2020/06/08 Python
Tensorflow中批量读取数据的案列分析及TFRecord文件的打包与读取
2020/06/30 Python
雅诗兰黛澳大利亚官网:Estée Lauder澳大利亚
2019/05/31 全球购物
中专自我鉴定范文
2013/10/16 职场文书
自考生自我评价分享
2014/01/18 职场文书
借款协议书范本
2014/04/22 职场文书
人力资源管理专业毕业生自荐书
2014/05/25 职场文书
我的中国梦演讲稿400字
2014/08/19 职场文书
php实例化对象的实例方法
2021/11/17 PHP
攻略丨滑雪究竟该选哪款对讲机?
2022/02/18 无线电
MySQL实现配置主从复制项目实践
2022/03/31 MySQL
SQL Server数据库备份和恢复数据库的全过程
2022/06/14 SQL Server