Discuss / Python / 新手打卡

新手打卡

Topic source

邹lh

#1 Created at ... [Delete] [Delete and Lock User]
res = {'city':'','forecast':[]}
class WeatherXmlhandler(object):


    def start_element(self, name, attrs):

        if 'location' in name:
            res['city'] = attrs['city']
        if 'forecast'in name:
            res['forecast'].append({'date':attrs['date'],'high': attrs['high'],'low' :attrs['low']})


    def end_element(self, name):
        pass

    def char_data(self, text):
        pass

def parseXml(xml_str):
    handler = WeatherXmlhandler()
    Parser = ParserCreate()
    Parser.StartElementHandler = handler.start_element
    Parser.EndElementHandler = handler.end_element
    Parser.CharacterDataHandler = handler.char_data
    Parser.Parse(xml_str)
    return res

  • 1

Reply