最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Python生成XML文件的方法

来源:动视网 责编:小采 时间:2020-11-27 14:25:07
文档

Python生成XML文件的方法

Python生成XML文件的方法:这篇文章主要介绍了使用Python生成XML的方法,结合具体实例形式详细分析了Python生成xml文件的具体流畅与相关注意事项,需要的朋友可以参考下本文实例讲述了使用Python生成XML的方法。分享给大家供大家参考,具体如下:1. bookstore.py#encodi
推荐度:
导读Python生成XML文件的方法:这篇文章主要介绍了使用Python生成XML的方法,结合具体实例形式详细分析了Python生成xml文件的具体流畅与相关注意事项,需要的朋友可以参考下本文实例讲述了使用Python生成XML的方法。分享给大家供大家参考,具体如下:1. bookstore.py#encodi


这篇文章主要介绍了使用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

<?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生成XML文件的方法

Python生成XML文件的方法:这篇文章主要介绍了使用Python生成XML的方法,结合具体实例形式详细分析了Python生成xml文件的具体流畅与相关注意事项,需要的朋友可以参考下本文实例讲述了使用Python生成XML的方法。分享给大家供大家参考,具体如下:1. bookstore.py#encodi
推荐度:
标签: 的方法 xml python
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top