Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:48:11+00:00 2026-05-29T19:48:11+00:00

Possible Duplicate: it's a google weather api?how to parse such kind of data using

  • 0

Possible Duplicate:
it's a google weather api?how to parse such kind of data using NSXML?

I am trying to make a weather app. I am able to get to google weather api that returns the following XML output (sorry for the bad formatting)

<?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>

I am able to get current temp what I am unable to get are the temp values for the rest of the days of the week from the XML. I read apple docs and googled this quite a bit but I am not sure how I can parse this string using apples NSXMLParser api. I don’t want to deal with other external parsers or include them in my project as my needs are very simple. Following is the code that I have implemented so far

- (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?
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:48:12+00:00Added an answer on May 29, 2026 at 7:48 pm

    If anyone else in future is interested in parsing this XML string, well here it is. Please rate my answer if it worked for you

    -(IBAction) GetLocalWeather
    {
    
      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];
    
    arryDataMutable = [[NSMutableArray alloc] init];
    
        //TEST
        NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
        [parser setDelegate:self];
        [parser parse];
    
        int arrySizInt = [arryDataMutable count];
        //NSLog(@"[arryDataMutable count]: %d", arrySizInt);
    
    
    
        /* Weather Conditions 
         Clear 
         Cloudy 
         Fog
         Haze 
         Light Rain
         Mostly Cloudy 
         Overcast 
         Partly Cloudy
         Rain 
         Rain Showers 
         Showers
         Thunderstorm 
         Chance of Showers 
         Chance of Snow 
         Chance of Storm 
         Mostly Sunny
         Partly Sunny 
         Scattered Showers 
         Sunny
         */
    
        //We will only get 4 day forecast including current day
    
        //initialize counter
        int counter = 0;
    
        //skip first entry, its always current day condition
        for (int i=1; i<arrySizInt; i++)
        {
            //NSLog(@"Array: %@", [arryDataMutable objectAtIndex:i]);
    
            NSString *dayStr = [[NSString alloc] initWithString:[arryDataMutable objectAtIndex:i]];
    
            //all week-days
            if ([dayStr isEqualToString:@"Mon"] ||
                [dayStr isEqualToString:@"Tue"] ||
                [dayStr isEqualToString:@"Wed"] ||
                [dayStr isEqualToString:@"Thu"] ||
                [dayStr isEqualToString:@"Fri"] ||
                [dayStr isEqualToString:@"Sat"] ||
                [dayStr isEqualToString:@"Sun"] )
            {
    
                //NSLog(@"counter: %d", counter);
    
                //get the next 3 values from the array
                //Its always going to be a pair of 4 values
                NSString *lowStr = [[NSString alloc] initWithString:[arryDataMutable objectAtIndex:(i+1)]];
                NSString *highStr = [[NSString alloc] initWithString:[arryDataMutable objectAtIndex:(i+2)]];
                NSString *conditionStr = [[NSString alloc] initWithString:[arryDataMutable objectAtIndex:(i+3)]];
    
                NSString *tempStrToDisp = [NSString stringWithFormat: @"%@ H:%@ L:%@", dayStr, highStr, lowStr];
    
    
                //cell-1
                if (counter == 0)
                {
    
                    textLable1.text = tempStrToDisp;
    
                }
    
                //cell-2
                if (counter == 1)
                {
    
                    textLable2.text = tempStrToDisp;
                }
    
                //cell-3
                if (counter == 2)
                {
    
                    textLable3.text = tempStrToDisp;
                }
    
                //cell-4
                if (counter == 3)
                {
    
                    textLable4.text = tempStrToDisp;
                }
    
                //increment
                //This will only get incremented 4 times
                counter = counter + 1;
            }
    
    
        }//End-for-loop
    }
    
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict 
    {
    
        //NSLog(@"XML Parser 1 ... elementName ... %@", elementName);
    
    
        if ([elementName isEqualToString:@"day_of_week"]) 
        { 
            NSString *tempStr = [attributeDict objectForKey:@"data"]; 
            //NSLog(@"day-of-week: %@", tempStr);
    
            [arryDataMutable addObject:tempStr];
    
        }
    
        if ([elementName isEqualToString:@"low"]) 
        { 
            NSString *tempStr = [attributeDict objectForKey:@"data"]; 
            [arryDataMutable addObject:tempStr];
    
        }
    
        if ([elementName isEqualToString:@"high"]) 
        { 
            NSString *tempStr = [attributeDict objectForKey:@"data"]; 
            [arryDataMutable addObject:tempStr];
    
        }
    
        if ([elementName isEqualToString:@"condition"]) 
        { 
            NSString *tempStr = [attributeDict objectForKey:@"data"]; 
            [arryDataMutable addObject:tempStr];
        }
    
    
    
    }
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
    {
    
        //NSLog(@"XML Parser 2 ...");
        //NSLog(@"string ... %@", string);
    
    }
    
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
    {
        //NSLog(@"XML Parser 3 ...");
        //NSLog(@"elementName: %@", elementName);
        //NSLog(@"namespaceURI: %@", namespaceURI);
        //NSLog(@"qName: %@", qName);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I parse JSON in Google App Engine? json module was
Possible Duplicate: How to get Google Analytics data using OAuth? I'm struggling with it
Possible Duplicate: How to change tooltip text for google chart api? I am using
Possible Duplicate: Android integrate google+ in app How to make google + integrate in
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Choosing Java vs Python on Google App Engine We are going to
Possible Duplicate: How can I use the Google App engine bulkloader to back up
Possible Duplicate: Can Google Maps/Places 'autocomplete' API be used via AJAX? There seems to
Possible Duplicate: A .net wrapper for Google App Engine? is there a C# .net
Possible Duplicate: Is there already a Google+ API? Is it possible to develop application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.