1

I wants use google weather api in my project. here I am struggling from long time. to get the current temperature. city, humidity,day of week etc

I successfully hit the url. but I not know exact what i do in foundCharacter method to fetch the value.

Thanks

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname       namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    if ([elementname isEqualToString:@"weather"]) 
    {
            currentTweet = [Tweet alloc];
    }
}

 - (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementname isEqualToString:@"city"]) 
     {

     }
 }
4

1 回答 1

1

请检查代码:

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname       namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementname isEqualToString:@"weather"]) 
    {
            currentTweet = [Tweet alloc];
    }
    else if ([elementname isEqualToString:@"city"]) 
    {
          currentTweet.city = [attributeDict valueForKey:@"city"]
    }
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
 {
    if ([elementname isEqualToString:@"weather"]) 
     {
           NSLog("Current City: %@",currentTweet.city);
     }
 }

对于这种类型的 xml,您不想使用parser:foundCharacters:

引用 NSXMLPareser 参考指南中有关foundCharacters方法的内容:

由解析器对象发送,以向其委托提供表示当前元素的全部或部分字符的字符串。

解析器对象可以向委托发送几个 parser:foundCharacters: 消息来报告元素的字符。因为字符串可能只是当前元素的全部字符内容的一部分,所以您应该将它附加到当前的字符累积中,直到元素发生变化。

有关更多详细信息:NSXML 解析器参考解析器的工作foundCharacters使用

于 2012-07-25T17:14:26.367 回答