Monday, 2 February 2015

Objective C + XML/SOAP

// .h File

#import <UIKit/UIKit.h> @interface ViewController : UIViewController<NSURLConnectionDataDelegate,NSURLConnectionDelegate,NSXMLParserDelegate> { NSString *curDescription; } @property (strong, nonatomic) NSMutableData *webResponseData; @end


// .m File

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSString *soapMessage = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" "<soap:Body>" "<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\">" "<Celsius>50</Celsius>" "</CelsiusToFahrenheit>" "</soap:Body>" "</soap:Envelope>"; NSString *soapMessage1 = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" "<soap:Body>" "<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\">" "<Celsius>%@</Celsius>" "</CelsiusToFahrenheit>" "</soap:Body>" "</soap:Envelope>" ,@"50"]; NSURL *requestURL = [NSURL URLWithString:@"http://www.w3schools.com/webservices/tempconvert.asmx"]; NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:requestURL]; NSString *messageLength = [NSString stringWithFormat:@"%d", (int)[soapMessage length]]; [myRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [myRequest addValue: @"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"]; [myRequest addValue: messageLength forHTTPHeaderField:@"Content-Length"]; [myRequest setHTTPMethod:@"POST"]; [myRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self]; if( theConnection ) { self.webResponseData = [NSMutableData data]; } else { NSLog(@"Some error occurred in Connection"); } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma marks- NSUrlConnection Delegate Methods Implementation:: -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [self.webResponseData setLength:0]; } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.webResponseData appendData:data]; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"Some error in your Connection. Please try again."); } -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"Received Bytes from server: %d", (int)[self.webResponseData length]); NSString *myXMLResponse = [[NSString alloc] initWithBytes: [self.webResponseData bytes] length:[self.webResponseData length] encoding:NSUTF8StringEncoding]; NSLog(@"myXMLResponse :: %@",myXMLResponse); // Parse XML NSData *myData = [myXMLResponse dataUsingEncoding:NSUTF8StringEncoding]; NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:myData]; // Don't forget to set the delegate! xmlParser.delegate = self; // Run the parser BOOL parsingResult = [xmlParser parse]; } #pragma mark - NSXMLParserDelaget Implementation :: -(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { curDescription = string; } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if([elementName isEqual: @"CelsiusToFahrenheitResult"]) NSLog(@"curDescription :: %@",curDescription); }

No comments:

Post a Comment