【问题标题】:XML parsing yahoo weather forecastXML解析雅虎天气预报
【发布时间】:2013-10-25 06:37:30
【问题描述】:

我正在为 iphone 制作一个应用程序,我想从以下 xml 显示当前温度和天气图像: http://xml.weather.yahoo.com/forecastrss/UKXX0085_c.xml

我能够解析 xml 并读取描述选项卡数据。但是我如何进一步解析它以从中获取温度和天气图像?

【问题讨论】:

    标签: ios xml parsing weather


    【解决方案1】:

    温度在yweather:condition。查看返回为 attributesdidStartElementNSDictionary

    温度的测量单位为yweather:units(同样在attributes 字典中)。

    图片链接包含在description 中。你真的应该使用 HTML 解析器,就像在How to Parse HTML on iOS 中讨论的那样,或者(喘气)你可以使用正则表达式:

    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(<img\\s[\\s\\S]*?src\\s*?=\\s*?['\"](.*?)['\"][\\s\\S]*?>)+?"
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    
    NSString *imageUrlString = nil;
    NSTextCheckingResult *match = [regex firstMatchInString:htmlString options:0 range:NSMakeRange(0, [htmlString length])];
    if (match)
        imageUrlString = [htmlString substringWithRange:[match rangeAtIndex:2]];
    

    【讨论】:

      猜你喜欢
      • 2013-02-09
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 2012-08-24
      • 2011-12-14
      • 2013-02-28
      • 2018-09-17
      相关资源
      最近更新 更多