使用Python生成XML的方法实例


Posted in Python onMarch 21, 2017

本文实例讲述了使用Python生成XML的方法。分享给大家供大家参考,具体如下:

1. bookstore.py

#encoding:utf-8
'''
根据一个给定的XML Schema,使用DOM树的形式从空白文件生成一个XML。
'''
from xml.dom.minidom import Document
doc = Document() #创建DOM文档对象
bookstore = doc.createElement('bookstore') #创建根元素
bookstore.setAttribute('xmlns:xsi',"http://www.w3.org/2001/XMLSchema-instance")#设置命名空间
bookstore.setAttribute('xsi:noNamespaceSchemaLocation','bookstore.xsd')#引用本地XML Schema
doc.appendChild(bookstore)
############book:Python处理XML之Minidom################
book = doc.createElement('book')
book.setAttribute('genre','XML')
bookstore.appendChild(book)
title = doc.createElement('title')
title_text = doc.createTextNode('Python处理XML之Minidom') #元素内容写入
title.appendChild(title_text)
book.appendChild(title)
author = doc.createElement('author')
book.appendChild(author)
author_first_name = doc.createElement('first-name')
author_last_name = doc.createElement('last-name')
author_first_name_text = doc.createTextNode('张')
author_last_name_text = doc.createTextNode('三')
author.appendChild(author_first_name)
author.appendChild(author_last_name)
author_first_name.appendChild(author_first_name_text)
author_last_name.appendChild(author_last_name_text)
book.appendChild(author)
price = doc.createElement('price')
price_text = doc.createTextNode('28')
price.appendChild(price_text)
book.appendChild(price)
############book1:Python写网站之Django####################
book1 = doc.createElement('book')
book1.setAttribute('genre','Web')
bookstore.appendChild(book1)
title1 = doc.createElement('title')
title_text1 = doc.createTextNode('Python写网站之Django')
title1.appendChild(title_text1)
book1.appendChild(title1)
author1 = doc.createElement('author')
book.appendChild(author1)
author_first_name1 = doc.createElement('first-name')
author_last_name1 = doc.createElement('last-name')
author_first_name_text1 = doc.createTextNode('李')
author_last_name_text1 = doc.createTextNode('四')
author1.appendChild(author_first_name1)
author1.appendChild(author_last_name1)
author_first_name1.appendChild(author_first_name_text1)
author_last_name1.appendChild(author_last_name_text1)
book1.appendChild(author1)
price1 = doc.createElement('price')
price_text1 = doc.createTextNode('40')
price1.appendChild(price_text1)
book1.appendChild(price1)
########### 将DOM对象doc写入文件
f = open('bookstore.xml','w')
f.write(doc.toprettyxml(indent = ''))
f.close()

2. bookstore.xsd

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xsd:element name="bookstore" type="bookstoreType"/>
 <xsd:complexType name="bookstoreType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="book" type="bookType"/>
  </xsd:sequence>
 </xsd:complexType>
 <xsd:complexType name="bookType">
  <xsd:sequence>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="authorName"/>
   <xsd:element name="price" type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="genre" type="xsd:string"/>
 </xsd:complexType>
 <xsd:complexType name="authorName">
  <xsd:sequence>
   <xsd:element name="first-name" type="xsd:string"/>
   <xsd:element name="last-name" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>
</xsd:schema>

3. 根据上面的XML Schema用Python minidom生成的XML

bookstore.xml

<?xml version="1.0" ?>
<bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="bookstore.xsd">
 <book genre="XML">
  <title>
   Python处理XML之Minidom
  </title>
  <author>
   <first-name>
    张
   </first-name>
   <last-name>
    三
   </last-name>
  </author>
  <price>
   28
  </price>
 </book>
 <book genre="Web">
  <title>
   Python写网站之Django
  </title>
  <author>
   <first-name>
    李
   </first-name>
   <last-name>
    四
   </last-name>
  </author>
  <price>
   40
  </price>
 </book>
</bookstore>
Python 相关文章推荐
python ElementTree 基本读操作示例
Apr 09 Python
使用cx_freeze把python打包exe示例
Jan 24 Python
简单介绍Python中的round()方法
May 15 Python
Python爬取网页中的图片(搜狗图片)详解
Mar 23 Python
pip install urllib2不能安装的解决方法
Jun 12 Python
python GUI实现小球满屏乱跑效果
May 09 Python
python多进程重复加载的解决方式
Dec 13 Python
python 实现屏幕录制示例
Dec 23 Python
Python爬虫程序架构和运行流程原理解析
Mar 09 Python
Python文本文件的合并操作方法代码实例
Mar 31 Python
关于Python3的import问题(pycharm可以运行命令行import错误)
Nov 18 Python
简述python四种分词工具,盘点哪个更好用?
Apr 13 Python
Python存取XML的常见方法实例分析
Mar 21 #Python
Python正则简单实例分析
Mar 21 #Python
Python处理XML格式数据的方法详解
Mar 21 #Python
Python做简单的字符串匹配详解
Mar 21 #Python
Python 转义字符详细介绍
Mar 21 #Python
python 迭代器和iter()函数详解及实例
Mar 21 #Python
浅谈五大Python Web框架
Mar 20 #Python
You might like
判断ie的两种简单方法
2013/08/12 Javascript
JS实现多物体缓冲运动实例代码
2013/11/29 Javascript
js中opener与parent的区别详细解析
2014/01/14 Javascript
父节点获取子节点的字符串示例代码
2014/02/26 Javascript
JavaScript实现Java中StringBuffer的方法
2015/02/09 Javascript
原生JS实现LOADING效果
2015/03/16 Javascript
jquery实现带缩略图的全屏图片画廊效果实例
2015/06/25 Javascript
javascript单页面手势滑屏切换原理详解
2016/03/21 Javascript
javascript实现方法调用与方法触发小结
2016/03/26 Javascript
NodeJs中express框架的send()方法简介
2017/06/20 NodeJs
JS实现搜索关键词的智能提示功能
2017/07/07 Javascript
nodejs简单访问及操作mysql数据库的方法示例
2018/03/15 NodeJs
解决vue js IOS H5focus无法自动弹出键盘的问题
2018/08/30 Javascript
Vue中JS动画与Velocity.js的结合使用
2019/02/13 Javascript
JS实现的杨辉三角【帕斯卡三角形】算法示例
2019/02/26 Javascript
4 种滚动吸顶实现方式的比较
2019/04/09 Javascript
JavaScript强制类型转换和隐式类型转换操作示例
2019/05/01 Javascript
vue 插件的方法代码详解
2019/06/06 Javascript
微信小程序 接入腾讯地图的两种写法
2021/01/12 Javascript
python paramiko利用sftp上传目录到远程的实例
2019/01/03 Python
Python实现二叉树的常见遍历操作总结【7种方法】
2019/03/06 Python
Python 获取 datax 执行结果保存到数据库的方法
2019/07/11 Python
python 对一幅灰度图像进行直方图均衡化
2020/10/27 Python
CSS3点击按钮实现背景渐变动画效果
2016/10/19 HTML / CSS
严选全球尖货,立足香港:Bonpont宝盆
2018/07/24 全球购物
了解AppleShare protocol(AppleShare协议)吗
2015/08/28 面试题
Linux机考试题
2015/10/16 面试题
大学生毕业求职简历的自我评价
2013/10/24 职场文书
微笑服务演讲稿
2014/05/13 职场文书
软件售后服务承诺书
2014/05/21 职场文书
计算机网络及管理学专业求职信
2014/06/05 职场文书
房屋买卖委托书格式范本格式
2014/10/13 职场文书
企业战略合作意向书
2015/05/08 职场文书
学雷锋活动简报
2015/07/20 职场文书
优秀学生主要事迹怎么写
2015/11/04 职场文书
涨工资申请书应该怎么写?
2019/07/08 职场文书