国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

使用Python生成XML的方法實例

2019-11-25 16:18:04
字體:
來源:轉載
供稿:網友

本文實例講述了使用Python生成XML的方法。分享給大家供大家參考,具體如下:

1. bookstore.py

#encoding:utf-8'''根據一個給定的XML Schema,使用DOM樹的形式從空白文件生成一個XML。'''from xml.dom.minidom import Documentdoc = Document() #創建DOM文檔對象bookstore = doc.createElement('bookstore') #創建根元素bookstore.setAttribute('xmlns:xsi',"http://www.w3.org/2001/XMLSchema-instance")#設置命名空間bookstore.setAttribute('xsi:noNamespaceSchemaLocation','bookstore.xsd')#引用本地XML Schemadoc.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.VeVB.COm/code/xmljson

在線格式化XML/在線壓縮XML
http://tools.VeVB.COm/code/xmlformat

XML在線壓縮/格式化工具:
http://tools.VeVB.COm/code/xml_format_compress

XML代碼在線格式化美化工具:
http://tools.VeVB.COm/code/xmlcodeformat

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python操作xml數據技巧總結》、《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盖州市| 平阴县| 神木县| 西林县| 邯郸县| 衡阳市| 鄂伦春自治旗| 区。| 邳州市| 衡山县| 陈巴尔虎旗| 祁连县| 泗水县| 陆丰市| 天等县| 武汉市| 新宁县| 浠水县| 吐鲁番市| 伊金霍洛旗| 乌兰浩特市| 巴南区| 外汇| 上高县| 巫溪县| 静宁县| 昌图县| 武功县| 黑河市| 蛟河市| 申扎县| 屏山县| 石首市| 中卫市| 大冶市| 霍城县| 宁明县| 双峰县| 泗水县| 邯郸市| 巴彦县|