rendered paste body#!/usr/bin/env python
from xml.sax import make_parser
from xml.sax.handler import ContentHandler
class QuoteHandler(ContentHandler):
def __init__ (self, searchTerm):
self.searchTerm= searchTerm;
self.isDataElement = 0;
self.HashArr = {'ById':{},'ByUsr':{}}
def getVars(self, which, search):
if which == 'ID':
try: print self.HashArr['ById'][str(search)]
except: print "search term %s not found" % str(search)
def startElement(self, name, attrs):
if name == 'quote':
self.quoteID = attrs.get('id',"")
self.quoteAuthor = attrs.get('author',"")
elif name == 'data':
self.isDataElement= 1;
self.quote = "";
def endElement(self, name):
if name == 'data':
self.isDataElement= 0
if name == 'quote': # and self.searchTerm == self.quoteID : print self.quote + self.quoteID +
self.HashArr['ById'][self.quoteID]=[self.quote, self.quoteAuthor]
print "self.quoteID: %s\nself.quoteAuthor: %s\nself.quote: %s" % (self.quoteID, self.quoteAuthor, self.quote)
def characters (self, ch):
if self.isDataElement== 1:
self.quote += ch
searchTerm = "1"
parser = make_parser()
curHandler = QuoteHandler(searchTerm)
parser.setContentHandler(curHandler)
parser.parse(open('/home/nero/kninja/files/xmltest'))
a = curHandler.getVars('ID', 1)