使用Python生成XML的方法实例
作者:WangShide 发布时间:2022-10-01 19:20:15
标签: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
<?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>
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程序设计有所帮助。
0
投稿
猜你喜欢
- pygal的安装大家可以参阅:pip和pygal的安装实例教程线图:import pygalline_chart = pygal.Line(
- reshape函数:改变数组的维数(注意不是shape大小)>>> e= np.arange(10)>>>
- documentFragment 是一個無父對象的document對象.他支持以下DOM2方法:appendChild, cloneNode
- 本文主要是介绍Go,从语言对比分析的角度切入。之所以选择与Python、Erlang对比,是因为做为高级语言,它们语言特性上有较大的相似性,
- 现在IE7已经推出一段时间并且渗透到用户当中,不用等太久我们就可以在页面上使用更高级的CSS。两个最有用的项目将是 Child子和Adjac
- import httplibimport osimport timedef check_http(i):
- 当然,如果你的网站文章中有图片,那么请记得一定要打上自己的LOGO,而且这个LOGO不要固定在这些图片的某个角落里,一定要随机出现在图片的任
- 通过引用serial模块包,来操作串口。1、查看串口名称在Linux和Windows中,串口的名字规则不太一样。需要事先查看。Linux下的
- 说明1、当函数的参数太多,需要简化时,使用functools.partial可以创建一个新的函数。2、这个新的函数可以固定原始函数的部分参数
- Python中对信号处理的模块主要是使用signal模块,但signal主要是针对Unix系统,所以在Windows平台上Python不能很
- Python 如何转换string到float?简单几步,让你轻松解决。打开软件,新建python项目,如图所示右键菜单中创建.py文件,如
- 一、输出指令ASP的输出指令<% =expression %>显示表达式的值。这个输出指令等同于使用Resp
- 一,PHP脚本与动态页面。 PHP脚本是一种服务器端脚本程序,可通过嵌入等方法与HTML文件混合,也可以类,函数封装等形式,以模板的方式对用
- 方法不是主流的。有一组数据,大概10万个左右,每一单位的值不会大于30000,要求按照由大到小的顺序不重复输出。参考无忧cosin的方法后(
- MySQL安全性指南(2) 作 者: 晏子2.1.3 数据库和表权限下列权限运用于数据库和表上的操作。ALTER允许你使用ALTER TAB
- PHP PDO 事务与自动提交现在通过 PDO 连接上了,在开始进行查询前,必须先理解 PDO 是如何管理事务的。事务支持四大特性(ACID
- 1.业务需求背景业务提供一张底层图片1以及需要在底层图片上添加的图片2,两张图片大小不一致,将小图2添加到底图1中,并在其他的空白部分添加个
- 要介绍Python的三元表达式,可以先看看其他编程语言比如C,JAVA中应用:public class java { public stat
- 注意:安装opencv之前需要先安装numpy,matplotlib等一、安装方法方法一、在线安装1.先安装opencv-pythonpip
- 目录1.字典的介绍2.访问字典的值(一)根据键访问值(二)通过get()方法访问值3.修改字典的值4.添加字典的元素(键值对)5.删除字典的