Splitting SAX results

IamIan iansan at gmail.com
Wed Jun 6 23:35:40 EDT 2007


Hi list,

I have a very simple SAX script from which I get results like
'Title1:Description','Title2:Description'. I want to split each result
on the colon, using the two resulting elements as key/value pairs in a
dictionary. I've tried a couple different approaches with lists etc,
but I keep getting an 'IndexError: list index out of range' when I go
to split the results. Probably an easy fix but it's my first hack at
SAX/XML. Thank you!

from xml.sax import make_parser
from xml.sax.handler import ContentHandler

class reportHandler(ContentHandler):
  def __init__(self):
    self.isReport = 0

  def startElement(self, name, attrs):
    if name == 'title':
      self.isReport = 1
      self.reportText = ''

  def characters(self, ch):
    if self.isReport:
      self.reportText += ch

  def endElement(self, name):
    if name == 'title':
      self.isReport = 0
      print self.reportText

parser = make_parser()
parser.setContentHandler(reportHandler())
parser.parse('http://www.some.com/rss/')




More information about the Python-list mailing list