SimpleApiForXml

http://sax.sourceinfo.new

XML Parsing을 위한 간단한 인터페이스. callback-based, stream-based API.

사용예)

   1 from xml.sax.handler import ContentHandler
   2 
   3 class ArticleHandler(ContentHandler):
   4     """
   5     A handler to deal with articles in XML
   6     """
   7     inArticle = False
   8     inBody = False
   9     isMatch = False
  10     title = ""
  11     body = ""
  12 
  13     def startElement(self, name, attrs):
  14         if name == "webArticle":
  15             subcat = attrs.get("subcategory", "")
  16             if subcat.find("tech") > -1:
  17                 self.inArticle = True
  18                 self.isMatch = True
  19         elif self.inArticle:
  20             if name == "header":
  21                 self.title = attrs.get("title","")
  22             if name == "body":
  23                 self.inBody = True
  24 
  25     def characters(self, chars):
  26         if self.inBody:
  27             if len(self.body) < 80:
  28                 self.body += chars
  29             if len(self.body) > 80:
  30                 self.body = self.body[:78] + "..."
  31                 self.inBody = False
  32 
  33     def endElement(self, name):
  34         if name == "body":
  35             self.inBody = False

article.xml

<?xml version="1.0"?>
<webArticle category="news" subcategory="technical">
    <header title="NASA Builds Warp Drive"
        length="3k" 
        autohr="Joe Reporter" 
        distribution="all"/>
    <body>Seattle, WA - Today an anonymous individual
        announced that NASA has completed building a
        Wrap Drive and has parked a ship that uses
        claims that although he hasn't been contacted by
        NASA concerning the parked space vessel, he assumes
        that he will be launching it later this week to
        mount an exhibition to the Andromeda Galaxy.
    </body>
</webArticle>

SAX (last edited 2011-09-16 08:11:59 by 211)

web biohackers.net