1

由于“&”字符,我的 NSXMLParsing 无法正常工作。我的代码如下。有什么帮助吗?

 NSString *myRequestString = [NSString stringWithFormat:@"http://abc.com/def/webservices/aa.php?family_id=%d",self.passFamilyId];
//NSLog(@"Requested Service = %@",myRequestString);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:myRequestString]];
[request setHTTPMethod: @"POST" ];
NSData *downloadedData = [ NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *str = [[NSString alloc] initWithData:downloadedData encoding:NSASCIIStringEncoding];
//NSString *contentString = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
NSData * data=[str dataUsingEncoding:NSUTF8StringEncoding];


//NSLog (@"string is :%@" ,str);    

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];   

// Call the XMLParsers's initXMLParse method.
ClientDetailXmlParser *parser = (ClientDetailXmlParser*)[[ClientDetailXmlParser alloc] initXMLParser];
[xmlParser setDelegate:parser];
BOOL success = [xmlParser parse];

// Check for XML parsing.
if(success) 
{
    NSLog(@"No Errors in clientDetailXml.xml");     
}
else 
{
    NSLog(@"Error Error Error in clientDetailXml.xml!!!");
}   

[parser release];
parser = nil;

if (objClientAddUpdate != nil) {
    [objClientAddUpdate createBubbleList];
}
4

3 回答 3

8

代替

NSString *str = [[NSString alloc] initWithData:downloadedData encoding:NSASCIIStringEncoding];
//NSString *contentString = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
NSData * data=[str dataUsingEncoding:NSUTF8StringEncoding];

和:

NSString *str = [[NSString alloc] initWithData:downloadedData encoding:NSASCIIStringEncoding];
NSString *contentString = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
NSData * data=[contentString dataUsingEncoding:NSUTF8StringEncoding];
于 2011-09-03T06:38:55.823 回答
0

与 sepehr-mahmoudian 的答案非常相似,但不是替换任何 & 你应该只替换 & 未转义的字符,为此你可以使用正则表达式:

NSString *str = [[NSString alloc] initWithData:downloadedData encoding:NSASCIIStringEncoding];
//NSString *contentString = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
NSData * data=[str dataUsingEncoding:NSUTF8StringEncoding];

和:

NSString *str = [[NSString alloc] initWithData:downloadedData encoding:NSASCIIStringEncoding];

NSRange searchedRange = NSMakeRange(0, [str length]);
NSString *pattern = @"&(?!(#[0-9]{2,4}|[A-z]{2,6});)";
NSError *error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
str = [regex stringByReplacingMatchesInString:str options:0 range:searchedRange withTemplate:@"&"];

NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
于 2019-11-09T00:23:18.253 回答
-1

这对我有用:

NSString *response = [[NSString alloc]initWithData:dataResponse encoding:NSUTF8StringEncoding];
NSString *newResponse = [response stringByReplacingOccurrencesOfString:@"&amp:" withString:@"AND"];
NSData *dataObj = [newResponse dataUsingEncoding:NSUTF8StringEncoding];

如果我用符号'&'替换它,响应将是正确的,但它会抛出解析错误:68。所以我不得不使用'and'。

于 2016-01-07T01:41:33.123 回答