如何在Objective C中使用XML解析发布带有特殊字符和泰语的字符串?
Posted
技术标签:
【中文标题】如何在Objective C中使用XML解析发布带有特殊字符和泰语的字符串?【英文标题】:How to post string with special character and Thai Language using XML parsing in Objective C? 【发布时间】:2017-09-18 06:07:54 【问题描述】:我是 ios 新手,在发布包含特殊字符的字符串时遇到问题。
我的代码在 DidFinishLoading 中是这样的:
NSXMLParser *myNSXMLParserPostObj=[[NSXMLParser alloc]initWithData:myNSMDataPostFromServer];
myNSXMLParserPostObj.delegate=self;
[myNSXMLParserPostObj parse];
NSLog(@"%@",myNSXMLParserPostObj.parserError);
NSString *responseStringWithEncoded = [[NSString alloc] initWithData: myNSMDataPostFromServer encoding:NSUTF8StringEncoding];
//NSLog(@"Response from Server : %@", responseStringWithEncoded);
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@ NSDocumentTypeDocumentAttribute: NShtmlTextDocumentType documentAttributes:nil error:nil];
serverResponse.attributedText = attrStr;
NSString *Str =serverResponse.text;
NSLog(@"Server Response =%@",Str);
UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:@"Success"
message:@"Complaint Added Successfully"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
为了从网络服务获取数据,我使用如下代码:
loginStatusZone = [[NSString alloc] initWithBytes: [myNSMDatazoneFromServer mutableBytes] length:[myNSMDatazoneFromServer length] encoding:NSUTF8StringEncoding];
NSLog(@"loginStatus =%@",loginStatusZone);
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:loginStatusZone error:&parseError];
NSLog(@"JSON DICTIONARY = %@",xmlDictionary);
recordResultZone = [xmlDictionary[@"success"] integerValue];
NSLog(@"Success: %ld",(long)recordResultZone);
NSDictionary* Address=[xmlDictionary objectForKey:@"soap:Envelope"];
NSLog(@"Address Dict = %@",Address);
NSDictionary *new =[Address objectForKey:@"soap:Body"];
NSLog(@"NEW DICT =%@",new);
NSDictionary *LoginResponse=[new objectForKey:@"FillZonesNewResponse"];
NSLog(@"Login Response DICT =%@",LoginResponse);
NSDictionary *LoginResult=[LoginResponse objectForKey:@"FillZonesNewResult"];
NSLog(@"Login Result =%@",LoginResult);
if(LoginResult.count>0)
NSLog(@"Login Result = %@",LoginResult);
NSLog(@"Login Result Dict =%@",LoginResult);
NSString *teststr =[[NSString alloc] init];
teststr =[LoginResult objectForKey:@"text"];
NSLog(@"Test String Value =%@",teststr);
NSString *string = [LoginResult valueForKey:@"text"];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
zoneresponsedict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
idzonearray=[[NSMutableArray alloc]init];
idzonearray=[zoneresponsedict valueForKey:@"ZoneId"];
NSLog(@"Result Array =%@",idzonearray);
shortnamezonearray=[[NSMutableArray alloc]init];
shortnamezonearray=[zoneresponsedict valueForKey:@"ZoneName"];
NSLog(@"Result Array =%@",shortnamezonearray);
如果我添加特殊字符,那么它会显示错误
Domain=NSXMLParserErrorDomain Code=111 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 111.)"
如何发布带有特殊字符的数据。
我正在用泰语添加这样的内容
但我得到了这个:
与印地语相同
输出是这样的
但是,当有人使用 android 或网站添加泰语时,我会得到正确的泰语文本。唯一的问题是这个邮政编码。
【问题讨论】:
你的特殊角色是什么,你能分享(部分)你的xml文件吗?错误代码 111 可能表示 xml 文件格式错误:***.com/questions/20454853/nsxmlparsererrordomain-111 @Koen 我将特殊字符添加为“&”。 在下面查看我的答案。 @Koen 不,我直接需要按原样传递它。因为我还需要用泰语传递。我不想替换字符串。 请阅读此内容,或许对您有帮助:***.com/questions/1091945/… 【参考方案1】:您可以替换您的字符串,以便发送特殊字符。可以使用字符串方法stringByReplacingOccurrencesOfString
NSString *RaisedBy="Your Special character string";
RaisedBy = [RaisedBy stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
RaisedBy = [RaisedBy stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
RaisedBy = [RaisedBy stringByReplacingOccurrencesOfString:@">" withString:@">"];
RaisedBy = [RaisedBy stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
RaisedBy = [RaisedBy stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
此代码还将支持泰语。
【讨论】:
【参考方案2】:您需要像在 html 文件中一样编写 &:
& < >
【讨论】:
以上是关于如何在Objective C中使用XML解析发布带有特殊字符和泰语的字符串?的主要内容,如果未能解决你的问题,请参考以下文章