【发布时间】:2013-10-25 06:37:30
【问题描述】:
我正在为 iphone 制作一个应用程序,我想从以下 xml 显示当前温度和天气图像: http://xml.weather.yahoo.com/forecastrss/UKXX0085_c.xml
我能够解析 xml 并读取描述选项卡数据。但是我如何进一步解析它以从中获取温度和天气图像?
【问题讨论】:
我正在为 iphone 制作一个应用程序,我想从以下 xml 显示当前温度和天气图像: http://xml.weather.yahoo.com/forecastrss/UKXX0085_c.xml
我能够解析 xml 并读取描述选项卡数据。但是我如何进一步解析它以从中获取温度和天气图像?
【问题讨论】:
温度在yweather:condition。查看返回为 attributes 的 didStartElement 的 NSDictionary。
温度的测量单位为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]];
【讨论】: