日韩欧乱色一区二区三区在线_久久―日本道色综合久久_欧美日本一区二区_网曝91综合精品门事件在线

千鋒教育-做有情懷、有良心、有品質的職業教育機構

手機站
千鋒教育

千鋒學習站 | 隨時隨地免費學

千鋒教育

掃一掃進入千鋒手機站

領取全套視頻
千鋒教育

關注千鋒學習站小程序
隨時隨地免費學習課程

當前位置:首頁  >  技術干貨  > 使用python自帶的xml.dom創建和解析xml

使用python自帶的xml.dom創建和解析xml

來源:千鋒教育
發布人:xqq
時間: 2023-11-07 14:27:58 1699338478

python中的xml.dom模塊使用的就是傳統的dom解析api和方法。所以也就不寫什么了,主要就是練習敲敲代碼,繼續熟悉python。本文通過xml.dom.minidom創建一個xml文檔,然后再解析出來,用以熟悉相關接口方法的使用。

創建一個xml文檔:

'''

Createdon2012-1-10

Createaxmldocument

@author:xiaojay

'''

fromxml.domimportminidom

doc=minidom.Document()

doc.appendChild(doc.createComment("Thisisasimplexml."))

booklist=doc.createElement("booklist")

doc.appendChild(booklist)

defaddBook(newbook):

book=doc.createElement("book")

book.setAttribute("id",newbook["id"])

title=doc.createElement("title")

title.appendChild(doc.createTextNode(newbook["title"]))

book.appendChild(title)

author=doc.createElement("author")

name=doc.createElement("name")

firstname=doc.createElement("firstname")

firstname.appendChild(doc.createTextNode(newbook["firstname"]))

lastname=doc.createElement("lastname")

lastname.appendChild(doc.createTextNode(newbook["lastname"]))

name.appendChild(firstname)

name.appendChild(lastname)

author.appendChild(name)

book.appendChild(author)

pubdate=doc.createElement("pubdate")

pubdate.appendChild(doc.createTextNode(newbook["pubdate"]))

book.appendChild(pubdate)

booklist.appendChild(book)

addBook({"id":"1001","title":"Anapple","firstname":"Peter","lastname":"Zhang","pubdate":"2012-1-12"})

addBook({"id":"1002","title":"Love","firstname":"Mike","lastname":"Li","pubdate":"2012-1-10"})

addBook({"id":"1003","title":"Steve.Jobs","firstname":"Tom","lastname":"Wang","pubdate":"2012-1-19"})

addBook({"id":"1004","title":"HarryPotter","firstname":"Peter","lastname":"Chen","pubdate":"2012-11-11"})

f=file("book.xml","w")

doc.writexml(f)

f.close()

通過doc.toprettyxml(indent,newl,encoding)方法可以優雅顯示xml文檔,但是要避免直接寫入文本,否則會給解析帶來麻煩,盡量使用自帶的writexml方法。

生成的文檔內容:

Peter

Zhang

2012-1-12

.................

解析該xml文檔:

'''

Createdon2012-1-10

Scanaxmldoc

@author:xiaojay

'''

fromxml.domimportminidom,Node

classbookscanner:

def__init__(self,doc):

forchildindoc.childNodes:

ifchild.nodeType==Node.ELEMENT_NODE\

andchild.tagName=="book":

bookid=child.getAttribute("id")

print"*"*20

print"Bookid:",bookid

self.handle_book(child)

defhandle_book(self,node):

forchildinnode.childNodes:

ifchild.nodeType==Node.ELEMENT_NODE:

ifchild.tagName=="title":

print"Title:",self.getText(child.firstChild)

ifchild.tagName=="author":

self.handle_author(child)

ifchild.tagName=="pubdate":

print"Pubdate:",self.getText(child.firstChild)

defgetText(self,node):

ifnode.nodeType==Node.TEXT_NODE:

returnnode.nodeValue

else:return""

defhandle_author(self,node):

author=node.firstChild

forchildinauthor.childNodes:

ifchild.nodeType==Node.ELEMENT_NODE:

ifchild.tagName=="firstname":

print"Firstname:",self.getText(child.firstChild)

ifchild.tagName=="lastname":

print"Lastname:",self.getText(child.firstChild)

doc=minidom.parse("book.xml")

forchildindoc.childNodes:

ifchild.nodeType==Node.COMMENT_NODE:

print"Conment:",child.nodeValue

ifchild.nodeType==Node.ELEMENT_NODE:

bookscanner(child)

以上內容為大家介紹了使用python自帶的xml.dom創建和解析xml,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。

tags: python培訓
聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
10年以上業內強師集結,手把手帶你蛻變精英
請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
免費領取
今日已有369人領取成功
劉同學 138****2860 剛剛成功領取
王同學 131****2015 剛剛成功領取
張同學 133****4652 剛剛成功領取
李同學 135****8607 剛剛成功領取
楊同學 132****5667 剛剛成功領取
岳同學 134****6652 剛剛成功領取
梁同學 157****2950 剛剛成功領取
劉同學 189****1015 剛剛成功領取
張同學 155****4678 剛剛成功領取
鄒同學 139****2907 剛剛成功領取
董同學 138****2867 剛剛成功領取
周同學 136****3602 剛剛成功領取
相關推薦HOT
主站蜘蛛池模板: 浠水县| 金昌市| 丽水市| 黔南| 眉山市| 波密县| 江永县| 庆云县| 铁岭县| 轮台县| 广宁县| 罗江县| 敖汉旗| 明光市| 北川| 嵩明县| 武冈市| 龙州县| 保亭| 西城区| 罗山县| 静安区| 泉州市| 江永县| 汉中市| 阜城县| 乌苏市| 和静县| 古蔺县| 彭阳县| 赤峰市| 西畴县| 庆城县| 永泰县| 清丰县| 罗定市| 民和| 青冈县| 泰和县| 南陵县| 祁门县|