-1
可能重複解析谷歌的天氣XML:
it's a google weather api?how to parse such kind of data using NSXML?使用的NSXMLParser
我試圖做一個天氣應用程序。我能得到一個返回下面的XML輸出(對不起,壞的格式),谷歌的天氣API
<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information><city data="Detroit, MI"/><postal_code data="Detroit"/><latitude_e6 data=""/><longitude_e6 data=""/><forecast_date data="2012-02-09"/><current_date_time data="2012-02-09 21:53:00 +0000"/><unit_system data="US"/></forecast_information><current_conditions><condition data="Clear"/><temp_f data="37"/><temp_c data="3"/><humidity data="Humidity: 48%"/><icon data="/ig/images/weather/sunny.gif"/><wind_condition data="Wind: SW at 15 mph"/></current_conditions><forecast_conditions><day_of_week data="Thu"/><low data="25"/><high data="40"/><icon data="/ig/images/weather/sunny.gif"/><condition data="Clear"/></forecast_conditions><forecast_conditions><day_of_week data="Fri"/><low data="13"/><high data="38"/><icon data="/ig/images/weather/snow.gif"/><condition data="Snow Showers"/></forecast_conditions><forecast_conditions><day_of_week data="Sat"/><low data="18"/><high data="23"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="Mostly Sunny"/></forecast_conditions><forecast_conditions><day_of_week data="Sun"/><low data="20"/><high data="27"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="Mostly Sunny"/></forecast_conditions></weather></xml_api_reply>
我能獲得當前溫度什麼我無法獲得對於其餘部分的溫度值XML中一週的日子。我讀了蘋果文檔,並用google搜索了一下,但我不確定如何使用蘋果NSXMLParser api解析這個字符串。我不想處理其他外部解析器或將它們包含在我的項目中,因爲我的需求非常簡單。以下是我迄今已實現的代碼
- (IBAction)GetCurrentWeather
{
NSString * location = @"Detroit";
NSString * address = @"http://www.google.co.uk/ig/api?weather=";
NSString * request = [NSString stringWithFormat:@"%@%@",address,location];
NSURL *URL = [NSURL URLWithString:request];
NSError *error;
NSString *XML = [NSString stringWithContentsOfURL:URL encoding:NSASCIIStringEncoding error:&error];
//NSLog(@"XML: %@", XML);
// Extract current temperature the 'dirty' way
//NSString *tempInC = [[[[XML componentsSeparatedByString:@"temp_c data=\""] objectAtIndex:1] componentsSeparatedByString:@"\""] objectAtIndex:0];
NSString *tempInF = [[[[XML componentsSeparatedByString:@"temp_f data=\""] objectAtIndex:1] componentsSeparatedByString:@"\""] objectAtIndex:0];
NSString *city = [[[[XML componentsSeparatedByString:@"city data=\""] objectAtIndex:1] componentsSeparatedByString:@"\""] objectAtIndex:0];
//NSString *condition = [[[[XML componentsSeparatedByString:@"condition data=\""] objectAtIndex:1] componentsSeparatedByString:@"\""] objectAtIndex:0];
NSString *humidity = [[[[XML componentsSeparatedByString:@"humidity data=\""] objectAtIndex:1] componentsSeparatedByString:@"\""] objectAtIndex:0];
NSString *wind = [[[[XML componentsSeparatedByString:@"wind_condition data=\""] objectAtIndex:1] componentsSeparatedByString:@"\""] objectAtIndex:0];
NSString *dayOfWeek = [[[[XML componentsSeparatedByString:@"day_of_week data=\""] objectAtIndex:1] componentsSeparatedByString:@"\""] objectAtIndex:0];
//NSLog(@"It's dayOfWeek %@", dayOfWeek);
//NSLog(@"It's tempInC %@ degrees", tempInC);
//NSLog(@"It's tempInF %@ degrees", tempInF);
//NSLog(@"It's city %@", city);
//NSLog(@"It's condition %@", condition);
NSString *tempStrToDisp = [NSString stringWithFormat: @"Temp: %@F", tempInF];
//THIS ALL WORKS!!!
textLable1.text = city;
textLable2.text = tempStrToDisp;
textLable3.text = humidity;
textLable4.text = wind;
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
[parser setDelegate:self];
[parser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
NSLog(@"XML Parser 1 ...");
NSLog(@"elementName ... %@", elementName);
//HOW TO GET rest of the week high/low temp VALUES HERE?
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"XML Parser 2 ...");
//HOW TO GET VALUES HERE?
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"XML Parser 3 ...");
//HOW TO GET VALUES HERE?
}
在此之前已經討論過多次 - 請搜索,或至少閱讀[這個問題](http://stackoverflow.com/questions/3306214/its-a -google-weather-apihow-to-parse-such-kind-of-data-using-nsxml),它在答案中有一個完整的代碼示例 – Jason 2012-02-10 01:15:35