washedev
washedev

Reputation: 107

SOAP response stored into NSString need to be parsed

I have this SOAP Request and my response has been stored into a NSString, I need to read this response and get data stored into a field. This is an example of my response:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><ReadDataResponse xmlns="http://sysman.it/webservices/"><ReadDataResult>success</ReadDataResult><Data><TableName>V_CP_NOTIFICATIONS_DEVICEID</TableName><values><ArrayOfCpFieldAndValue><cpFieldAndValue><Name /><Value>0</Value><Type>IntegerType</Type><CompOperator>=</CompOperator></cpFieldAndValue></ArrayOfCpFieldAndValue></values><keys><cpFieldAndValue><Name>DEVICE_ID</Name><Value>1006be49b73a98af</Value><Type>VarcharType</Type><CompOperator>=</CompOperator></cpFieldAndValue><cpFieldAndValue><Name>PUSH_READCNT</Name><Value>0</Value><Type>IntegerType</Type><CompOperator>=</CompOperator></cpFieldAndValue></keys><orClause>false</orClause></Data></ReadDataResponse></soap:Body></soap:Envelope>

<cpFieldAndValue><Name /><Value>0</Value> I need that number to be stored into a variable. This is my code at the moment:

NSData *postData = [sSOAPMessage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%lu", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"----"]];
[request setHTTPMethod:@"POST"];
[request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"Request reply: %@", requestReply);

}] resume];

Thanks for the help.

Upvotes: 1

Views: 69

Answers (2)

Md. Ibrahim Hassan
Md. Ibrahim Hassan

Reputation: 5477

Well you can refer to this SO Answer. You have to copy the XMLParser.h and XMLParser.m files and use as follows:

NSString *xmlString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:xmlString];
NSInteger value = [[xmlDoc valueForKeyPath:@"soap:Body.ReadDataResponse.Data.values.ArrayOfCpFieldAndValue.cpFieldAndValue.Value"] integerValue];
NSLog(@"%d", value);

Output:

0

Upvotes: 1

user1376400
user1376400

Reputation: 624

You have to use NSXMLParser

https://developer.apple.com/documentation/foundation/nsxmlparser

Please find some example code explains below

https://www.ioscreator.com/tutorials/parsing-xml-tutorial

Upvotes: 0

Related Questions