[代碼筆記] python 之 xml解析_sax:基於事件驅動的解析方式

#!usr/bin/python3
#文件名: demo_xml.py

#SAX解析

import xml.sax

#繼承並重寫xml.sax.ContentHandler 的衆多方法事件回調
class MovieHandler(xml.sax.ContentHandler ):
    def __init(self):#經測試,在類中任何有self的地方直接使用self.變量名賦值初始化變量,相當與子啊類中定義一個變量,即下面的所有代碼註釋後加pass後程序也可以正常運行
        #'''
        self.CurrentData=""
        self.type=''
        self.format=''
        self.yesr=''
        self.rating=''
        self.stars=''
        self.description=''
        #'''

    #元素開始時調用(重寫方法)
    def startElement(self,tag,attributes):
        self.CurrentData=tag
        if tag=='movie':
            print('*****Movie*****')
            title=attributes['title']
            print('Title:',title)

    #元素結束是調用:當遇到結束標籤時,說明character方法以調用過了,可以打印記錄下的值
    def endElement(self,tag):

        if tag=='type':
            print('Type:',self.type)
        elif tag=='format':
            print('Format:',self.format)
        elif tag=='rating':
            print('Rating:',self.rating)
        elif tag=='stars':
            print('Stars:',self.stars)
        elif tag=='description':
            print('Description:',self.description)
        self.CurrentData=''

    #讀取字符時調用
    '''
        1.character方法,在遇到字符串時調用
        2.此方法在不同時機被調用時,參數content的值代表意義不同:
            a.開頭到遇到標籤過程中:爲開頭到標籤之間的字符
            b.一個標籤到下一個標籤過程中: 爲2個標籤之間的字符
            c.一個標籤到尾部過程中: 爲標籤到結尾間的字符
            開頭---標籤1---標籤2---標籤N...-----結尾
        3.可以通過在開始標籤是記錄下標籤的名稱tag,來判斷其值是屬於那個標籤的
    '''
    def characters(self,content):
        if self.CurrentData=='type':
            self.type=content
        elif self.CurrentData=='format':
            self.format=content
        elif self.CurrentData=='rating':
            self.rating=content
        elif self.CurrentData=='stars':
            self.stars=content
        elif self.CurrentData=='description':
            self.description=content

if (__name__=='__main__'):
    parser=xml.sax.make_parser()
    parser.setFeature(xml.sax.handler.feature_namespaces,0)

    Handler=MovieHandler()
    parser.setContentHandler(Handler)

    parser.parse('demo.xml')

實例所用xml代碼(摘自菜鳥教程):

<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>

“`

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章