2012-07-25 177 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"]);