使用Python產生XML

來源:互聯網
上載者:User
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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.