python生成xml文件_使用Python生成XML的方法实例

本文实例讲述了使用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

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

bookstore.xml

Python处理XML之Minidom

28

Python写网站之Django

40

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具:http://tools.jb51.net/code/xmljson

在线格式化XML/在线压缩XML:http://tools.jb51.net/code/xmlformat

XML在线压缩/格式化工具:http://tools.jb51.net/code/xml_format_compress

XML代码在线格式化美化工具:http://tools.jb51.net/code/xmlcodeformat

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python操作xml数据技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

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

本文标题: 使用Python生成XML的方法实例

本文地址: http://www.cppcns.com/jiaoben/python/182729.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值