如何在Objective C中使用XML解析发布带有特殊字符和泰语的字符串? [英] How to post string with special character and Thai Language using XML parsing in Objective C?
问题描述
我是iOS的新手,我在发布包含特殊字符的字符串时遇到问题。
I am new in iOS and I am facing problem regarding to post string containing special character.
我的代码在DidFinishLoading中是这样的:
My code is like this in 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];
为了从网络服务获取数据,我使用的代码如下:
For getting data from web service I am using code like this:
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);
}
如果我添加特殊字符,则会显示错误
If I add special character then it shows me error as
Domain=NSXMLParserErrorDomain Code=111 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 111.)"
如何发布具有特殊字符的数据。
How can I post data with special character.
我正在添加这样的内容泰语语言
I am adding something like this in Thai language
但我得到了这个:
与印地语相同
输出是这样的
但是当有人使用Android或网站添加泰语时,我会用泰语获得正确的文字。唯一的问题是这个邮政编码。
But when anyone add Thai language using Android or website I get proper text in Thai. Only problem is with this post code.
推荐答案
您需要按如下方式编写&<>,就像在一个html文件:
You will need to write the &<> as follows, just like in an html file:
& < >
这篇关于如何在Objective C中使用XML解析发布带有特殊字符和泰语的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!