python中的xml.dom模塊使用的就是傳統(tǒng)的dom解析api和方法。所以也就不寫什么了,主要就是練習(xí)敲敲代碼,繼續(xù)熟悉python。本文通過xml.dom.minidom創(chuàng)建一個xml文檔,然后再解析出來,用以熟悉相關(guān)接口方法的使用。
創(chuàng)建一個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)方法可以優(yōu)雅顯示xml文檔,但是要避免直接寫入文本,否則會給解析帶來麻煩,盡量使用自帶的writexml方法。
生成的文檔內(nèi)容:
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)
以上內(nèi)容為大家介紹了使用python自帶的xml.dom創(chuàng)建和解析xml,希望對大家有所幫助,如果想要了解更多Python相關(guān)知識,請關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。