zoukankan      html  css  js  c++  java
  • [python 学习] sax

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    import xml.sax
    
    class MovieHandler( xml.sax.ContentHandler ):
       def __init__(self):
             self.stack = []
       # 元素开始事件处理
       def startElement(self, tag, attributes):
          if len(self.stack) !=0:
                print "
    ","*"*4*len(self.stack),
          attr = ""
          # 属性值
          if attributes.getLength() > 0:
                for attrName in attributes.getNames():
                      attr = " " + attr + attrName + "=" + attributes.getValue(attrName)
          print "<"+tag+attr+">",
          self.stack.append("start")
    
       # 元素结束事件处理
       def endElement(self, tag):
          if self.stack[len(self.stack)-1] == "content" and self.stack[len(self.stack)-2] == "start":
                del self.stack[len(self.stack)-1]
                del self.stack[len(self.stack)-2]
          elif self.stack[len(self.stack)-1] =="start":
                del self.stack[len(self.stack)-1]
                print "
    ","*"*4*len(self.stack),
    
          print "</"+tag+">",
    
       # 内容事件处理
       def characters(self, content):
          if content.strip() != '': 
                self.stack.append("content")
                print content,
      
    if ( __name__ == "__main__"):
       
       # 创建一个 XMLReader
       parser = xml.sax.make_parser()
       # turn off namepsaces
       parser.setFeature(xml.sax.handler.feature_namespaces, 0)
    
       # 重写 ContextHandler
       Handler = MovieHandler()
       parser.setContentHandler( Handler )
       
       parser.parse("movie.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>

    解析输出:

    参见:http://www.cnblogs.com/hongfei/p/python-xml-sax.html

  • 相关阅读:
    Webservice学习之新建一个最简单的Webservice项目
    初学程序一定要养成良好的习惯
    你晚上睡好了吗?
    如何面对失恋?
    多病之秋少言多饮
    转:避开秋季相冲食物
    转:饭后九不要包你保健康
    转:五官不适预示五脏衰弱
    转:过度疲劳的27个信号与预防方法
    foxmail是不是不行了?
  • 原文地址:https://www.cnblogs.com/natian-ws/p/7795729.html
Copyright © 2011-2022 走看看