Python網路編程基礎筆記-使用minidom產生XML檔案

來源:互聯網
上載者:User

1.使用minidom建立XML檔案

#
-*- coding: cp936-*-
"""
使用minidom產生XML
1.建立Element,createElement
2.添加子節點,appendChild
3.建立Text,createTextNode
4.建立屬性,createAttribute

 res = minidom.Document()
    query = res.createElement("queryItems")
    query.setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance')
    query.setAttribute('xsi:schemaLocation','http://www.taobao.com/schema/personalhomepage queryItems.xsd')
    query.setAttribute('xmlns','http://www.xxx.com/schema/personalhomepage')
    query.setAttribute('xmlns:header','http://www.xxx.com/schema/personalhomepage/extend/headerType')

"""
from xml.dom
import minidom,Node

# 建立Document
doc = minidom.Document()
# 建立book節點
book = doc.createElement("book")
doc.appendChild(book)
# 建立Title節點
title = doc.createElement("title")
text = doc.createTextNode("Sample XML Thing")
title.appendChild(text)
book.appendChild(title)
# 建立author節點
author = doc.createElement("author")
# 建立name節點
name = doc.createElement("name")
first = doc.createElement("first")
first.appendChild(doc.createTextNode("Benjamin"))
name.appendChild(first)

last = doc.createElement("last")
last.appendChild(doc.createTextNode("Smith"))
name.appendChild(last)

author.appendChild(name)
book.appendChild(author)
# author節點完畢

# 建立chapter節點
chapter = doc.createElement("chapter")
chapter.setAttribute("number","1")
title = doc.createElement("title")
title.appendChild(doc.createTextNode("Fisrt Chapter"))
chapter.appendChild(title)

para = doc.createElement("para")
para.appendChild(doc.createTextNode("I think widgets are great.you should buy lots \
of them from"))
company = doc.createElement("company")
company.appendChild(doc.createTextNode("Springy widgets,Inc"))
para.appendChild(company)

chapter.appendChild(para)
# chapter節點完畢
book.appendChild(chapter)
# book節點完畢

print doc.toprettyxml(indent=
" ")

2.產生的XML檔案

<?xmlversion="1.0"
?>
<book>
    <title>
        Sample XML Thing
    </title>
    <author>
        <name>
            <first>
                Benjamin
            </first>
            <last>
                Smith
            </last>
        </name>
    </author>
    <chapter number="1">
        <title>
            Fisrt Chapter
        </title>
        <para>
            I think widgets are great.you should buy lots of them from
            <company>
                Springy widgets,Inc
            </company>
        </para>
    </chapter>
</book>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.