使用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程序设计有所帮助。


猜你喜欢
- Python作为一种脚本语言,其要求强制缩进,使其易读、美观,它的数据类型可以实现自动转换,而不需要像C、Java那样给变量定义数据类型,使
- PHP addslashes() 函数实例在每个双引号(")前添加反斜杠:<?php $str = addslashes(&
- 本文实例讲述了gearman+mysql方式实现持久化操作。分享给大家供大家参考,具体如下:1、为什么要持久化?gearman的job se
- 今天从网上学习了有关SQL注入的基本技能。SQL注入的重点就是构造SQL语句,只有灵活的运用SQL 语句才能构造出牛比的注入字符串。学完之后
- 1、装饰器装饰器(Decorator):从字面上理解,就是装饰对象的器件。可以在不修改原有代码的情况下,为被装饰的对象增加新的功能或者附加限
- 本文实例讲述了python元祖和字典的内建函数。分享给大家供大家参考,具体如下:元组Tuple元组是序列类型一种,也是不可变类型数据结构,对
- 本文实例为大家分享了python pygame模块编写飞机大战的具体代码,供大家参考,具体内容如下该程序没有使用精灵组,而是用列表存储对象来
- 如果 select 元素下的所有 option 元素均没有指定 selected 属性,会默认选中第一个。可以通过 select.selec
- 问题setInterval 是间隔调用,与之类似的还有 setTimeout。这两个 API 通常用来做 ajax 短连接轮询数据。比如有一
- 无论何时,IE总是让页面制作者感到那么的黯然销魂,尤其是IE6,IE7次之,虽然IE8已经做了很大的改进,但由于XP用户的数量实在太大,而且
- pyecharts介绍pyecharts是python与echarts链接,一个用于生成Echarts图标的第三方库,pyecharts分为
- 示例代码:<span style="font-size:18px;">function hi(){ var
- 操作步骤1.下载BeautifulReport文件,本例文件下载地址 最新文件下载地址2.复制文件BeautifulReport,至pyth
- 内容概要:如何把列表中的元素拼接为一个字符串呢?本文介绍了采用 join() 函数的解决方法。问题:有一个列表,比如:letters=[&a
- 前言Github源码地址本文同时也是学习唐宇迪老师深度学习课程的一些理解与记录。文中代码是实现在TensorFlow下使用卷积神经网络(CN
- 在使用Django过程中需要开发一些API给其他系统使用,为了安全把Token等验证信息放在header头中。如何获取:使用request.
- 历史:Message Queue的需求由来已久,80年代最早在金融交易中,高盛等公司采用Teknekron公司的产品,当时的Message
- 一、Pandas如何对Categorical类型字段数据统计实战场景:对Categorical类型字段数据统计,Categorical类型是
- 一 例子现在,讲述一个真实的故事!故事一定是伴随着赵忠祥老师的声音开始的,雨季就要来临了,又到了动物们 * 的季节了...还记得,之前发生的作
- MySQL短链接怎么设置1.查看mysql连接数语句命令:2.首先作为超级用户登录到MYSQL,注意必须是超级用户,否则后面会提示没有修改权