NSXMLParser 与“&”(&)字符的问题

发布于 2024-12-02 18:33:46 字数 1387 浏览 1 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荭秂 2024-12-09 18:33:46

替换

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

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];
陌伤ぢ 2024-12-09 18:33:46

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

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];
清晰传感 2024-12-09 18:33:46

这对我有用:

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

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

Here's what worked for me:

NSString *response = [[NSString alloc]initWithData:dataResponse encoding:NSUTF8StringEncoding];
NSString *newResponse = [response stringByReplacingOccurrencesOfString:@"&:" 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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文