NSXMLParser problem with “&”(Ampersand) character

对着背影说爱祢 提交于 2019-12-01 10:49:13

问题


My NSXMLParsing is not working because of "&" character.. my code s below .any help please ?

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

回答1:


Replace

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

With:

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



回答2:


Very similar to sepehr-mahmoudian's answer, but instead of replace any & you should really just replace & characters that are unescaped, to do this you can use a regexp:

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

With:

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



回答3:


Here's what worked for me:

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

If I replaced it with symbol '&', the response would be correct, but it would throw parsing error: 68. So I had to use 'and'.



来源:https://stackoverflow.com/questions/7291534/nsxmlparser-problem-with-ampersand-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!