python解析xml文件操作實(shí)例
本文實(shí)例講述了python解析xml文件操作的實(shí)現(xiàn)方法。分享給大家供大家參考。具體方法如下:
xml文件內(nèi)容如下:
<?xml version="1.0" ?>
<!--Simple xml document__chapter 8-->
<book>
<title>
sample xml thing
</title>
<author>
<name>
<first>
ma
</first>
<last>
xiaoju
</last>
</name>
<affiliation>
Springs Widgets, Inc.
</affiliation>
</author>
<chapter number="1">
<title>
First
</title>
<para>
I think widgets are greate.You should buy lots of them forom
<company>
Spirngy Widgts, Inc
</company>
</para>
</chapter>
</book>
python代碼:
from xml.dom import minidom, Node
import re, textwrap
class SampleScanner:
""""""
def __init__(self, doc):
"""Constructor"""
assert(isinstance(doc, minidom.Document))
for child in doc.childNodes:
if child.nodeType == Node.ELEMENT_NODE and \
child.tagName == "book":
self.handle_book(child)
def handle_book(self, node):
for child in node.childNodes:
if child.nodeType != Node.ELEMENT_NODE:
continue
if child.tagName == "title":
print "Book titile is:", self.gettext(child.childNodes)
if child.tagName == "author":
self.handle_author(child)
if child.tagName == "chapter":
self.handle_chapter(child)
def handle_chapter(self, node):
number = node.getAttribute("number")
print "number:", number
title_node = node.getElementsByTagName("title")
print "title:", self.gettext(title_node)
for child in node.childNodes:
if child.nodeType != Node.ELEMENT_NODE:
continue
if child.tagName == "para":
self.handle_chapter_para(child)
def handle_chapter_para(self, node):
company = ""
company = self.gettext(node.getElementsByTagName("company"))
print "chapter:para:company", company
def handle_author(self, node):
for child in node.childNodes:
if child.nodeType != Node.ELEMENT_NODE:
continue
if child.tagName == "name":
self.handle_author_name(child)
if child.tagName == "affiliation":
print "affiliation:", self.gettext(child.childNodes)
def handle_author_name(self, node):
first = ""
last = ""
for child in node.childNodes:
if child.nodeType != Node.ELEMENT_NODE:
continue
if child.tagName == "first":
first = self.gettext(child.childNodes)
if child.tagName == 'last':
last = self.gettext(child.childNodes)
print "firstname:%s,lastname:%s" % (first, last)
def gettext(self, nodelist):
retlist = []
for node in nodelist:
if node.nodeType == Node.TEXT_NODE:
retlist.append(node.wholeText)
elif node.hasChildNodes:
retlist.append(self.gettext(node.childNodes))
return re.sub('\s+', " ", ''.join(retlist))
if __name__=="__main__":
doc = minidom.parse("simple.xml")
sample = SampleScanner(doc)
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python3與fastdfs分布式文件系統(tǒng)如何實(shí)現(xiàn)交互
這篇文章主要介紹了Python3與fastdfs分布式文件系統(tǒng)如何實(shí)現(xiàn)交互,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Python錯(cuò)誤提示:[Errno 24] Too many open files的分析與解決
這篇文章主要給大家介紹了Python中出現(xiàn)錯(cuò)誤提示:[Errno 24] Too many open files的分析與解決,需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02
解決python中os.listdir()函數(shù)讀取文件夾下文件的亂序和排序問題
今天小編就為大家分享一篇解決python中os.listdir()函數(shù)讀取文件夾下文件的亂序和排序問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python實(shí)現(xiàn)查詢某個(gè)目錄下修改時(shí)間最新的文件示例
這篇文章主要介紹了Python實(shí)現(xiàn)查詢某個(gè)目錄下修改時(shí)間最新的文件,涉及Python使用os與shutil模塊針對(duì)文件的遍歷、屬性獲取、讀寫等相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
PyQt5+QtChart實(shí)現(xiàn)繪制極坐標(biāo)圖
QChart是一個(gè)QGraphicScene中可以顯示的QGraphicsWidget。本文將利用QtChart實(shí)現(xiàn)極坐標(biāo)圖的繪制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12
Django通用類視圖實(shí)現(xiàn)忘記密碼重置密碼功能示例
今天小編就為大家分享一篇Django通用類視圖實(shí)現(xiàn)忘記密碼重置密碼功能示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python實(shí)現(xiàn)將目錄中TXT合并成一個(gè)大TXT文件的方法
這篇文章主要介紹了Python實(shí)現(xiàn)將目錄中TXT合并成一個(gè)大TXT文件的方法,涉及Python針對(duì)目錄下文本文件的遍歷、讀取及寫入等技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
利用Opencv實(shí)現(xiàn)圖片的油畫特效實(shí)例
這篇文章主要給大家介紹了關(guān)于利用Opencv實(shí)現(xiàn)圖片的油畫特效的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

