欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python生成XML的方法實例

 更新時間:2017年03月21日 14:17:49   作者:WangShide  
這篇文章主要介紹了使用Python生成XML的方法,結合具體實例形式詳細分析了Python生成xml文件的具體流暢與相關注意事項,需要的朋友可以參考下

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

1. bookstore.py

#encoding:utf-8
'''
根據(jù)一個給定的XML Schema,使用DOM樹的形式從空白文件生成一個XML。
'''
from xml.dom.minidom import Document
doc = Document() #創(chuàng)建DOM文檔對象
bookstore = doc.createElement('bookstore') #創(chuàng)建根元素
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') #元素內(nèi)容寫入
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寫網(wǎng)站之Django####################
book1 = doc.createElement('book')
book1.setAttribute('genre','Web')
bookstore.appendChild(book1)
title1 = doc.createElement('title')
title_text1 = doc.createTextNode('Python寫網(wǎng)站之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. 根據(jù)上面的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寫網(wǎng)站之Django
  </title>
  <author>
   <first-name>
    李
   </first-name>
   <last-name>
    四
   </last-name>
  </author>
  <price>
   40
  </price>
 </book>
</bookstore>

PS:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:

在線XML/JSON互相轉(zhuǎn)換工具:
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相關內(nèi)容感興趣的讀者可查看本站專題:《Python操作xml數(shù)據(jù)技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python Socket編程技巧總結》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

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

相關文章

  • 跟老齊學Python之畫圈還不簡單嗎?

    跟老齊學Python之畫圈還不簡單嗎?

    畫圈?換一個說法就是循環(huán)。循環(huán),是高級語言編程中重要的工作。現(xiàn)實生活中,很多事情都是在循環(huán),日月更迭,斗轉(zhuǎn)星移,無不是循環(huán);王朝更迭,尋常百姓,也都是循環(huán)。
    2014-09-09
  • 連接pandas以及數(shù)組轉(zhuǎn)pandas的方法

    連接pandas以及數(shù)組轉(zhuǎn)pandas的方法

    今天小編就為大家分享一篇連接pandas以及數(shù)組轉(zhuǎn)pandas的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • 基于Python實現(xiàn)倒計時工具

    基于Python實現(xiàn)倒計時工具

    這篇文章主要為大家詳細介紹了基于Python實現(xiàn)倒計時工具,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • python 實現(xiàn)壓縮和解壓縮的示例

    python 實現(xiàn)壓縮和解壓縮的示例

    這篇文章主要介紹了python 實現(xiàn)壓縮和解壓縮的示例,幫助大家更好的利用python處理文件,感興趣的朋友可以了解下
    2020-09-09
  • python+selenium 腳本實現(xiàn)每天自動登記的思路詳解

    python+selenium 腳本實現(xiàn)每天自動登記的思路詳解

    這篇文章主要介紹了python+selenium 腳本實現(xiàn)每天自動登記,本文你給大家分享基本的思路,通過實例代碼截圖的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • 探索Python3.4中新引入的asyncio模塊

    探索Python3.4中新引入的asyncio模塊

    這篇文章主要介紹了Python3.4中新引入的asyncio模塊,包括其對端口和服務器等的操作,需要的朋友可以參考下
    2015-04-04
  • python實現(xiàn)將元祖轉(zhuǎn)換成數(shù)組的方法

    python實現(xiàn)將元祖轉(zhuǎn)換成數(shù)組的方法

    這篇文章主要介紹了python實現(xiàn)將元祖轉(zhuǎn)換成數(shù)組的方法,涉及Python中l(wèi)ist方法的使用技巧,需要的朋友可以參考下
    2015-05-05
  • 詳解Python中openpyxl模塊基本用法

    詳解Python中openpyxl模塊基本用法

    這篇文章主要介紹了Python中openpyxl模塊基本用法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Python中join函數(shù)簡單代碼示例

    Python中join函數(shù)簡單代碼示例

    這篇文章主要介紹了Python中join函數(shù)簡單代碼示例,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • python MD5加密的示例

    python MD5加密的示例

    這篇文章主要介紹了python MD5加密的示例,幫助大家更好的利用python進行加密,感興趣的朋友可以了解下
    2020-10-10

最新評論