千锋教育-做有情怀、有良心、有品质的职业教育机构

使用python自带的xml.dom创建和解析xml

来源:千锋教育
发布时间:2023-11-06 17:17:18
分享

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培训机构:千锋教育。

声明:本站部分稿件版权来源于网络,如有侵犯版权,请及时联系我们。

相关推荐

  • python丰富的内置类型及相关操作 python的内置类型如下:lString:字符串放在单引号、双引号、三引号(多行时)中,从0开始索引,支持n查:find、index、n切片:s[0:2]、s[1:]n连接:“abc”+“ef”=>
  • python强大的标准库 Python拥有一个强大的标准库。Python语言的核心只包含数字、字符串、列表、字典、文件等常见类型和函数,而由Python标准库提供了系统管理、网络通信、文本处理、数据库接口、图形系统、XML处理
  • python线程同步(锁) 多线程的优势在于可以同时运行多个任务(至少感觉起来是这样)。但是当线程需要共享数据时,可能存在数据不同步的问题。考虑这样一种情况:一个列表里所有元素都是0,线程"set"从后向前把所有元素改成1,而线
  • python访问对象的属性 以下列出了几个内建方法,可以用来检查或是访问对象的属性。这些方法可以用于任意对象而不仅仅是例子中的Cat实例对象;Python中一切都是对象。cat=Cat('kitty')print
  • python之使用inspect模块 inspect模块提供了一系列函数用于帮助使用自省。下面仅列出较常用的一些函数,想获得全部的函数资料可以查看inspect模块的文档。1.检查对象类型is{module|class|function|
  • Python 枚举 1.枚举的定义首先,定义枚举要导入enum模块。枚举定义用class关键字,继承Enum类。用于定义枚举的class和定义类的class是有区别【下一篇博文继续分享】。示例代码:fromenumimp