iPhone:解析 Google 天气 API 时遇到问题

发布于 2024-12-20 10:29:35 字数 611 浏览 1 评论 0原文

我正在通过 API 下载 Google 天气。我在解析时遇到问题。

使用 NSXMLParserDelegate,它找到元素“forecast_conditions”,但我似乎无法提取它的内容。解析器似乎认为“高”和“低”等是单独的元素。

这是我尝试解析的一个元素:

<forecast_conditions><day_of_week data="Sun"/><low data="20"/><high data="38"/><icon data="/ig/images/weather/sunny.gif"/><condition data="Clear"/></forecast_conditions>

我很惊讶......

`- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

 NSLog(@"foundCharacters string: %@",string);

}`

也没有返回任何内容。我认为这解析了元素 Help 的内容

I'm downloading Google weather through the API. I'm having trouble parsing.

Using NSXMLParserDelegate, it finds the element "forecast_conditions," but I cannot seem to extract it's content. The parsers seems to think "high" and "low" etc are separate elements.

This is an element I'm try to parse:

<forecast_conditions><day_of_week data="Sun"/><low data="20"/><high data="38"/><icon data="/ig/images/weather/sunny.gif"/><condition data="Clear"/></forecast_conditions>

I'm surprised ...

`- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

 NSLog(@"foundCharacters string: %@",string);

}`

doesn't return anything either. I thought this parsed the contents of an element

Help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔落惊风雨 2024-12-27 10:29:35

事实证明,解析器会自动创建“attributeDict”字典。循环elementNames,只需要调用[attributeDict objectForKey:@"data"]即可。下面的例子。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

// current conditions

if ([elementName isEqualToString:@"current_conditions"]) {

     currentConditions = [[NSMutableString alloc] init];
     temp_f = [[NSMutableString alloc] init];
     temp_c = [[NSMutableString alloc] init];
     humidity = [[NSMutableString alloc] init];
     currentIcon = [[NSMutableString alloc] init];
     wind_condition = [[NSMutableString alloc] init];

}

if ([elementName isEqualToString:@"condition"]) {
    [currentConditions appendString:[attributeDict objectForKey:@"data"]];
}
if ([elementName isEqualToString:@"temp_f"]) {
    [temp_f appendString:[attributeDict objectForKey:@"data"]];
}
if ([elementName isEqualToString:@"temp_c"]) {
    [temp_c appendString:[attributeDict objectForKey:@"data"]];
}
if ([elementName isEqualToString:@"humidity"]) {
    [humidity appendString:[attributeDict objectForKey:@"data"]];
}
if ([elementName isEqualToString:@"currentIcon"]) {
    [currentIcon appendString:[attributeDict objectForKey:@"data"]];
}
if ([elementName isEqualToString:@"wind_condition"]) {
    [wind_condition appendString:[attributeDict objectForKey:@"data"]];
}

It turns out the parser creates the "attributeDict" dictionary automatically. Cycling through the elementNames, you only need to call [attributeDict objectForKey:@"data"]. Example below.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

// current conditions

if ([elementName isEqualToString:@"current_conditions"]) {

     currentConditions = [[NSMutableString alloc] init];
     temp_f = [[NSMutableString alloc] init];
     temp_c = [[NSMutableString alloc] init];
     humidity = [[NSMutableString alloc] init];
     currentIcon = [[NSMutableString alloc] init];
     wind_condition = [[NSMutableString alloc] init];

}

if ([elementName isEqualToString:@"condition"]) {
    [currentConditions appendString:[attributeDict objectForKey:@"data"]];
}
if ([elementName isEqualToString:@"temp_f"]) {
    [temp_f appendString:[attributeDict objectForKey:@"data"]];
}
if ([elementName isEqualToString:@"temp_c"]) {
    [temp_c appendString:[attributeDict objectForKey:@"data"]];
}
if ([elementName isEqualToString:@"humidity"]) {
    [humidity appendString:[attributeDict objectForKey:@"data"]];
}
if ([elementName isEqualToString:@"currentIcon"]) {
    [currentIcon appendString:[attributeDict objectForKey:@"data"]];
}
if ([elementName isEqualToString:@"wind_condition"]) {
    [wind_condition appendString:[attributeDict objectForKey:@"data"]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文