SOAP Message request and XMLParsing in iOS app

– (void)viewDidLoad
{
[super viewDidLoad];
NSString *soapMessage = [NSString stringWithFormat:@”<?xml version=\”1.0\” encoding=\”utf-8\”?>\n”
“<soap12:Envelope xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\” xmlns:xsd=\”http://www.w3.org/2001/XMLSchema\” xmlns:soap12=\”http://www.w3.org/2003/05/soap-envelope\”>\n”
“<soap12:Body>\n”
“<ReceivLogintoken xmlns=\”http://tempuri.org/\”>\n”
“<username>%@</username>\n”
“<password>%@</password>\n”
“</ReceivLogintoken>\n”
“</soap12:Body>\n”
“</soap12:Envelope>”,userName,password];


NSString *urlString = [NSString stringWithFormat:@”http://admin.docketlaw.com/CourtJudristication.asmx”%5D;
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@”%d”, [soapMessage length]];
[theRequest addValue: @”text/xml; charset=utf-8″ forHTTPHeaderField:@”Content-Type”];
[theRequest addValue: msgLength forHTTPHeaderField:@”Content-Length”];
[theRequest setHTTPMethod:@”POST”];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( connection )
{
mutableData = [[NSMutableData alloc] init];
}
}
Now we have to add NSURLConnection delegates in your app like below,
#pragma mark –
#pragma mark NSURLConnection delegates
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
[mutableData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[mutableData appendData:data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[mutableData release];
[connection release];
// If we get any connection error we can manage it here…
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@”Alert” message:@”No Network Connection” delegate:self cancelButtonTitle:nil otherButtonTitles:@”OK”,nil];
[alertView show];
[alertView release];
return;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *xmlResponseString = [[NSString alloc] initWithBytes: [mutableData mutableBytes] length:[mutableData length] encoding:NSUTF8StringEncoding];
NSLog(@”Response in XML : %@”, xmlResponseString);
// In iOS apple provide NSXMLParser feature to parse XML data.
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:mutableData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
[connection release];
[mutableData release];
}
Now the time to add NSXMLParser delegates in our app like below. The XMLParsing should be based on your XML responses from webservice. Please modify this below sample code for your webservice response. This is just giving some basic idea about XMLParsing but won’t solve your problem.
#pragma mark –
#pragma mark NSXMLParser delegates
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@”LoginTokenTag”]) // This is the only tag i was received from my webservice response.
{
if (!xmlResultString) // xmlResultString is NSMutableString.
{
xmlResultString = [[NSMutableString alloc] init];
recordResults = YES; // Here we are changing the boolean value to YES because we should recognize whether our corresponding tag has reached.
}
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (recordResults) // If boolean is in YES we are ready to get the tag value.
{
[xmlResultString appendString:string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@”LoginTokenTag”])
{
[loginTokenArray addObject:[xmlResultString retain]]; //Here loginTokenArray is my NSMutableArray. Am storing the loginToken in this array from XMLParsing.
[self performSelector:@selector(xmlParsingFinished)]; //Already i told that i have only one tag. So this will be a last parsing tag. Now we have terminate the parsing process here by calling our custom method.
}
xmlResultString = nil;
[xmlResultString release];
}
-(void) xmlParsingFinished
{
// Here you can do your functions.
// If you receive multiple tags from the webservice here you load the values in your UITableView in here.
}
Previous
Next Post »