使用python自带的xml.dom创建和解析xml
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培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python中的filter函数功能是什么?
python中的filter函数功能是什么?在python中,面对众多的数据,我们要过滤筛选出我们需要的数据。python中的filter函数就是起到了过滤筛选的作...详情>>
2023-11-10 20:37:27
pythontime模块是什么
pythontime模块是什么在python中使用时间,就免不了和time模块打交道,另外两个模块这个暂时先不做介绍。做time模块的使用上,我们可以用它来对...详情>>
2023-11-10 15:53:16
python是什么编程语言
python是什么编程语言1、说明是一种面向对象、解释型计算机程序设计语言,由GuidovanRossum于1989年底发明,第一个公开发行版发行于1991年。Pyt...详情>>
2023-11-10 15:21:05
python异常处理的两种技巧
python异常处理的两种技巧1、传递异常有时我们会在捕捉到一个异常后重新引发它(传递异常),实现起来很简单,使用不带参数的raise语句即可。deff...详情>>
2023-11-10 14:49:39热门推荐
python中的filter函数功能是什么?
沸python delattr函数如何使用?
热python中pdb模块怎么用?
热Python如何截图保存?
新python中缺少module怎么办?
python strftime和strptime的不同分析
python time.strptime的格式化
python中@contextmanager是什么?
python对象的三要素是什么
pythonGIL在Python多线程的应用
python如何对多个CSV文件进行读取
pythonif嵌套命令如何理解?
python对列表进行永久性或临时排序的方法
python生成器调用方法引发异常
技术干货






