溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python使用sax模塊解析XML文件示例

發(fā)布時間:2020-09-24 10:26:14 來源:腳本之家 閱讀:161 作者:薔薇Nina 欄目:開發(fā)技術

本文實例講述了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ù),并增加一個當前數(shù)據(jù)
    self.CurrentData = ""
    self.type = ""
    self.format = ""
    self.year = ""
    self.rating = ""
    self.stars = ""
    self.description = ""
  # 文檔啟動的時候調用
  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))
  # 內容事件處理
  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
  # 元素結束事件處理
  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 = ""
  # 文檔結束的時候調用
  def endDocument(self):
    print('XML文檔解析結束!')
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:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:

在線XML/JSON互相轉換工具:
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

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python操作xml數(shù)據(jù)技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python Socket編程技巧總結》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設計有所幫助。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI