最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python生成XML的方法實(shí)例

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

本文實(shí)例講述了使用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")#設(shè)置命名空間
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:這里再為大家提供幾款關(guān)于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

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python操作xml數(shù)據(jù)技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Django JSonResponse對象的實(shí)現(xiàn)

    Django JSonResponse對象的實(shí)現(xiàn)

    本文主要介紹了Django JSonResponse對象的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python2手動安裝更新pip過程實(shí)例解析

    Python2手動安裝更新pip過程實(shí)例解析

    這篇文章主要介紹了Python2手動安裝更新pip過程實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • 利用django model save方法對未更改的字段依然進(jìn)行了保存

    利用django model save方法對未更改的字段依然進(jìn)行了保存

    這篇文章主要介紹了利用django model save方法對未更改的字段依然進(jìn)行了保存,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python統(tǒng)計(jì)字符串中指定字符出現(xiàn)次數(shù)的方法

    python統(tǒng)計(jì)字符串中指定字符出現(xiàn)次數(shù)的方法

    這篇文章主要介紹了python統(tǒng)計(jì)字符串中指定字符出現(xiàn)次數(shù)的方法,涉及Python中count函數(shù)的使用技巧,非常具有實(shí)用價值,需要的朋友可以參考下
    2015-04-04
  • Python實(shí)戰(zhàn)之ATM取款機(jī)的實(shí)現(xiàn)

    Python實(shí)戰(zhàn)之ATM取款機(jī)的實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了如何利用Python語言模擬實(shí)現(xiàn)ATM取款機(jī)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-09-09
  • python中函數(shù)的返回值及類型詳解

    python中函數(shù)的返回值及類型詳解

    這篇文章主要為大家介紹了python中函數(shù)的返回值及類型詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • python連接mysql數(shù)據(jù)庫示例(做增刪改操作)

    python連接mysql數(shù)據(jù)庫示例(做增刪改操作)

    python連接mysql數(shù)據(jù)庫示例,提供創(chuàng)建表,刪除表,數(shù)據(jù)增、刪、改,批量插入操作,大家參考使用吧
    2013-12-12
  • Python調(diào)整圖像hue值結(jié)合ImageEnhance庫以實(shí)現(xiàn)色調(diào)增強(qiáng)

    Python調(diào)整圖像hue值結(jié)合ImageEnhance庫以實(shí)現(xiàn)色調(diào)增強(qiáng)

    這篇文章主要介紹了Python調(diào)整圖像hue值結(jié)合ImageEnhance庫以實(shí)現(xiàn)色調(diào)增強(qiáng),PIL庫中的ImageEnhance類可用于圖像增強(qiáng),可以調(diào)節(jié)圖像的亮度、對比度、色度和銳度,通過RGB到HSV的變換加調(diào)整可以對圖像的色調(diào)進(jìn)行調(diào)整,需要的朋友可以參考下
    2023-09-09
  • Python如何使用pathlib模塊處理文件路徑

    Python如何使用pathlib模塊處理文件路徑

    這篇文章主要介紹了Python如何使用pathlib模塊處理文件路徑,我們將從創(chuàng)建 Path 對象、絕對路徑與相對路徑、訪問文件路徑分量,以及檢查文件路徑是否存在等幾個方面進(jìn)行講解,需要的朋友可以參考下
    2024-10-10
  • Python虛擬環(huán)境遷移的實(shí)現(xiàn)

    Python虛擬環(huán)境遷移的實(shí)現(xiàn)

    本文主要介紹了Python虛擬環(huán)境遷移的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03

最新評論

通化县| 渝北区| 思茅市| 关岭| 沧州市| 达州市| 确山县| 方正县| 亚东县| 乌鲁木齐市| 富川| 商洛市| 威远县| 上蔡县| 佛山市| 五莲县| 宁海县| 兰考县| 怀集县| 宁明县| 阳信县| 舞钢市| 桓仁| 合水县| 广汉市| 揭阳市| 郸城县| 五寨县| 南江县| 七台河市| 阜平县| 尼勒克县| 忻州市| 武平县| 巩留县| 沛县| 江陵县| 蓬安县| 拜城县| 中牟县| 龙胜|