0

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.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Jeremy
  • 883
  • 1
  • 20
  • 41
  • **The Google weather API was shut down in 2012** -> http://stackoverflow.com/questions/12145820/google-weather-api-gone/35943521 – John Slegers Mar 11 '16 at 16:01

1 Answers1

0

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"]];
}
Jeremy
  • 883
  • 1
  • 20
  • 41