Skip to content

Instantly share code, notes, and snippets.

@splbio
Created November 14, 2015 06:00
Show Gist options
  • Save splbio/7ea10d52fbd694348ef8 to your computer and use it in GitHub Desktop.
Save splbio/7ea10d52fbd694348ef8 to your computer and use it in GitHub Desktop.
import xml.sax
class FartHandler(xml.sax.ContentHandler):
FART_LEVEL = 3 # the schema is bad and you should feel bad...
def __init__(self):
self.lvl = 0
self.infarts = False
self.curfart = {}
self.chars = ""
self.nodename = ""
self.allfarts = []
#print "started..."
def startElement(self, name, attrs):
#print "start element %s, lvl %d " % (name, self.lvl)
self.lvl += 1
if self.lvl == FartHandler.FART_LEVEL and name == "fart":
#print "Entered a fart... level: %d" % self.lvl
self.infarts = True
if not self.infarts:
return
self.nodename = name
#print "new node: " + name
def characters(self, content):
self.chars += content
def endElement(self, name):
self.lvl -= 1
if self.infarts:
if self.lvl < FartHandler.FART_LEVEL:
if "content" not in self.curfart:
self.curfart = {}
return
self.allfarts.append(self.curfart)
self.curfart = {}
#print "======Exited fart======="
else:
self.curfart[name] = self.chars
self.chars = ""
fp = open("farts.xml", "r")
data = fp.read()
parser = xml.sax.make_parser()
Handler = FartHandler()
parser.setContentHandler( Handler )
parser.parse("farts.xml")
#print "Got %d farts" % len(Handler.allfarts)
search = "fart"
search = search.upper()
for fart in Handler.allfarts:
if search in fart["content"].upper() or search in fart["title"].upper():
print fart["content"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment