Python使用sax模塊解析XML文件示例
本文實例講述了Python使用sax模塊解析XML文件。分享給大家供大家參考,具體如下:
XML樣例:
<?xml version="1.0"?>
<collection shelf="New Arrivals">
<movie title="Enemy Behind">
<type>War, Thriller</type>
<format>DVD</format>
<year>2003</year>
<rating>PG</rating>
<stars>10</stars>
<description>Talk about a US-Japan war</description>
</movie>
<movie title="Transformers">
<type>Anime, Science Fiction</type>
<format>DVD</format>
<year>1989</year>
<rating>R</rating>
<stars>8</stars>
<description>A schientific fiction</description>
</movie>
<movie title="Trigun">
<type>Anime, Action</type>
<format>DVD</format>
<episodes>4</episodes>
<rating>PG</rating>
<stars>10</stars>
<description>Vash the Stampede!</description>
</movie>
<movie title="Ishtar">
<type>Comedy</type>
<format>VHS</format>
<rating>PG</rating>
<stars>2</stars>
<description>Viewable boredom</description>
</movie>
</collection>
SAX解析代碼展示:
from xml import sax
class MovieHandler(sax.ContentHandler):
def __init__(self):
# 初始化數(shù)據(jù),并增加一個當(dāng)前數(shù)據(jù)
self.CurrentData = ""
self.type = ""
self.format = ""
self.year = ""
self.rating = ""
self.stars = ""
self.description = ""
# 文檔啟動的時候調(diào)用
def startDocument(self):
print('XML開始解析中...')
# 元素開始事件處理
def startElement(self, name, attrs):
self.CurrentData=name
if self.CurrentData=='movie':
print('*********movie*********')
title=attrs['title']
print('Title:{0}'.format(title))
# 內(nèi)容事件處理
def characters(self, content):
if self.CurrentData == "type":
self.type = content
elif self.CurrentData == "format":
self.format = content
elif self.CurrentData == "year":
self.year = content
elif self.CurrentData == "rating":
self.rating = content
elif self.CurrentData == "stars":
self.stars = content
elif self.CurrentData == "description":
self.description = content
# 元素結(jié)束事件處理
def endElement(self, name):
if self.CurrentData=='type':
print('Type:{0}'.format(self.type))
elif self.CurrentData=='format':
print('Format:{0}'.format(self.format))
elif self.CurrentData=='year':
print('Year:{0}'.format(self.year))
elif self.CurrentData == 'rating':
print('Rating:{0}'.format(self.rating))
elif self.CurrentData == 'stars':
print('Stars:{0}'.format(self.stars))
elif self.CurrentData == 'description':
print('Description:{0}'.format(self.description))
self.CurrentData = ""
# 文檔結(jié)束的時候調(diào)用
def endDocument(self):
print('XML文檔解析結(jié)束!')
if __name__=='__main__':
handler=MovieHandler()
parser = sax.make_parser()
# parser.setFeature(sax.handler.feature_namespaces, 0)
parser.setContentHandler(handler)
parser.parse("sax_test.xml")
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python操作xml數(shù)據(jù)技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python 實現(xiàn)保存最新的三份文件,其余的都刪掉
今天小編就為大家分享一篇python 實現(xiàn)保存最新的三份文件,其余的都刪掉,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python數(shù)據(jù)結(jié)構(gòu)之圖的應(yīng)用示例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之圖的應(yīng)用,結(jié)合實例形式分析了Python數(shù)據(jù)結(jié)構(gòu)中圖的定義與遍歷算法相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
基于Python3 逗號代碼 和 字符圖網(wǎng)格(詳談)
下面小編就為大家?guī)硪黄赑ython3 逗號代碼 和 字符圖網(wǎng)格(詳談)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
python中threading.Semaphore和threading.Lock的具體使用
python中的多線程是一個非常重要的知識點,本文主要介紹了python中threading.Semaphore和threading.Lock的具體使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2023-08-08
Python+pyplot繪制帶文本標(biāo)注的柱狀圖方法
今天小編就為大家分享一篇Python+pyplot繪制帶文本標(biāo)注的柱狀圖方法,具有很好的價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
使用OpenCV獲取圖片連通域數(shù)量,并用不同顏色標(biāo)記函
這篇文章主要介紹了使用OpenCV獲取圖片連通域數(shù)量,并用不同顏色標(biāo)記函,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python+matplotlib繪制不同大小和顏色散點圖實例
這篇文章主要介紹了Python+matplotlib繪制不同大小和顏色散點圖實例,matplotlib的用法是比較多種多樣的,下面一起看看其中的一個實例吧2018-01-01

