我有此SOAP请求,并且我的响应已存储在NSString中,我需要读取此响应并将数据存储到字段中。这是我的回应的一个例子:
<?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>我需要将该数字存储到变量中。这是我目前的代码:

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];

谢谢您的帮助。

最佳答案

好吧,你可以参考这个SO Answer。您必须复制XMLParser.hXMLParser.m文件并按以下方式使用:

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);

输出:
0

10-08 06:12