How to make a NSURL Request and parse the XML response?


   
#import "ViewController.h"
#import "XMLReader.h"


      
     //first you need to create a URL using String.
      NSURL *url = [NSURL URLWithString:@"feed://feeds.feedburner.com/blogspot/MKGLf?format=xml"];
           
      //Once you create a URL,you are ready make a request.
       NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
       //After making request the apparent thing is expecting the response that may be expected response or an Error. so create those objects and initialize them with NULL.
        NSURLResponse *response = NULL;
NSError *requestError = NULL;
      //Once you have response with you, capture your responseData using NSData.
       NSData *responseData = [NSURLConnection sendSynchronousRequest:request        returningResponse:&response error:&requestError];

        //Convert the response data into response String.
NSString *responseString  = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

        //Now you can start parsing the data using XML parser. You need XML parser in-order to     use the below class method "dictionaryForXMLString"
NSError *parserError = NULL;

      
NSDictionary *xmlDict = [XMLReader dictionaryForXMLString:responseString error:NULL]; 

        //Once You have xmlDict handy, you can pass this to the any ViewController(like tableview) to populate the data.

       

7 comments: