Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Google Weather API parse XML

bigdave1024bigdave1024 Posts: 75Registered Users
edited September 2010 in iPhone SDK Development
Hello all,

I am working on a part of my app that displays the weather for a saved location. I am wanting to use the Google Weather API and all I care about is the data for the current conditions.

So far I am using NSURLConnection to access the web and pull the XML data from google. I have yet to implement the XML parsing part because I am unsure how to go about doing in. So far I have been able to retrieve the data and save it to a NSMutableData object however I'm a little unsure how to go from here. I've been researching using NSXMLParser and would like some advice on how I should implement it to capture the current conditions and save it to a NSString object. Here is the relevant parts of my code so far:
- (void)viewDidLoad {
	
	URLString = [NSString stringWithFormat:@"http://www.google.com/ig/api?weather=%@", currentLocation];
	NSLog(@"urlstring: %@", URLString);
	
	NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
	NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
	if (theConnection) {
		
		responseData = [[NSMutableData alloc] retain];
	}

	
	[super viewDidLoad];
	
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
	[responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	NSLog(@"Receiving data...");
	[responseData appendData:data];
	NSLog(@"responsedata: %@",responseData);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
	NSLog(@"Reverse Geocode request to Google failed!");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	NSLog(@"Finished loading...");
	[connection release];

}

Any help and advice would be greatly appreciated. Thanks!
Post edited by bigdave1024 on

Replies

  • bigdave1024bigdave1024 Posts: 75Registered Users
    edited February 2010
    I've been working on this some more trying to follow Apple's sample code SeismicXML and I am getting all confused because the code looks definitely like overkill for what I need to do.

    Essentially I need to extract the current conditions from a link like this:

    http://www.google.com/ig/api?alt=weather=cupertino,%20ca%20usa

    I would very much appreciate any help anyone can offer to get me pointed in the right direction. Thanks!
  • satyaprasadsatyaprasad Posts: 1New Users
    edited September 2010
    Hello all,

    I am working on a part of my app that displays the weather for a saved location. I am wanting to use the Google Weather API and all I care about is the data for the current conditions.

    So far I am using NSURLConnection to access the web and pull the XML data from google. I have yet to implement the XML parsing part because I am unsure how to go about doing in. So far I have been able to retrieve the data and save it to a NSMutableData object however I'm a little unsure how to go from here. I've been researching using NSXMLParser and would like some advice on how I should implement it to capture the current conditions and save it to a NSString object. Here is the relevant parts of my code so far:
    - (void)viewDidLoad {
    	
    	URLString = [NSString stringWithFormat:@"http://www.google.com/ig/api?weather=%@", currentLocation];
    	NSLog(@"urlstring: %@", URLString);
    	
    	NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    	NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    	if (theConnection) {
    		
    		responseData = [[NSMutableData alloc] retain];
    	}
    
    	
    	[super viewDidLoad];
    	
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    	[responseData setLength:0];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    	NSLog(@"Receiving data...");
    	[responseData appendData:data];
    	NSLog(@"responsedata: %@",responseData);
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    	NSLog(@"Reverse Geocode request to Google failed!");
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    	NSLog(@"Finished loading...");
    	[connection release];
    
    }
    

    Any help and advice would be greatly appreciated. Thanks!




    Hi,

    Now you have the NSData, right?
    You can covert that NSData to NSString by using the following code

    NSString* aStr;
    aStr = [[NSString alloc] initWithData:aData encoding:NSASCIIStringEncoding];

    Then you need to create an NSXMLParser object and you need to parse that NSString.

    While parsing there are some delegate methods will be called. You need to implement those delegate mehods based on your requirement.

    You can get more details about the NSXMLParser in the XCode Developer Documentation.

    Thanks,
    Satya
Sign In or Register to comment.