class WeatherSaxHandler(object): def __init__(self): self.text = {'city': None, 'country': None, 'today': {'text': None, 'low': None, 'high': None}, 'tomorrow': {'text': None, 'low': None, 'high': None}} def start_element(self, name, attrs): if name == 'yweather:forecast' and self.text['today']['text'] == None: self.text['today']['text'] = attrs['text'] self.text['today']['low'] = int(attrs['low']) self.text['today']['high'] = int(attrs['high']) elif name == 'yweather:forecast' and self.text['tomorrow']['text'] == None: self.text['tomorrow']['text'] = attrs['text'] self.text['tomorrow']['low'] = int(attrs['low']) self.text['tomorrow']['high'] = int(attrs['high']) elif name == 'yweather:location': self.text['city'] = attrs['city'] self.text['country'] = attrs['country'] def end_element(self, name): pass def char_data(self, name): pass def parse_weather(xml): handler = WeatherSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml) return handler.text
Sign in to make a reply
siusuu