2012-07-25 148 views
-2

我是Iphone App开发中的新手。我正在我的应用程序中实施Google Weather API。我使用教程代码,它使用NSXMLParser进行分析。我在下面的方法中删除了教程代码。我使用这个谷歌API http://www.google.com/ig/api?weather=nabha如何解析XML文件

现在我做什么来获得输出。当前温度,高温,低温等。

任何提示和解决方案真的很感激。

谢谢

我的方法是。

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


NSLog(@"Processing Element: %@", elementName); 


} 

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


} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 


} 





    2012-07-25 19:20:14.654 XML[4555:f803] Processing Element: xml_api_reply 
Current language: auto; currently objective-c 
Processing Element: weather 
Processing Element: forecast_information 
Processing Element: city 
Processing Element: postal_code 
Processing Element: latitude_e6 
Processing Element: longitude_e6 
Processing Element: forecast_date 
Processing Element: current_date_time 
Processing Element: unit_system 
Processing Element: current_conditions 
Processing Element: condition 
Processing Element: temp_f 
Processing Element: temp_c 
Processing Element: humidity 
Processing Element: icon 
Processing Element: wind_condition 

    etc...... 
    2012-07-25 19:20:44.042 XML[4555:f803] No Errors 

回答

0

我建议使用RaptureXML来解析短的XML片段。获得当前的条件温度看起来像这样(未经测试):

RXMLElement *root = [RXMLElement elementFromURL:[NSURL URLWithString:@"http://www.google.com/ig/api?weather=nabha"]]; 

RXMLElement *currentConditions = [root child:@"current_conditions"]; 
RXMLElement *tempC= [currentConditions child:@"temp_c"]; 
NSLog(@"temperature (C): %@", [tempC attribute:@"data"]);