提问人:Muju 提问时间:4/21/2017 最后编辑:halferMuju 更新时间:5/11/2019 访问量:1311
如何在目标 C 中使用 XML 解析发布带有特殊字符和泰语的字符串?
How to post string with special character and Thai Language using XML parsing in Objective C?
问:
我是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];
为了从 Web 服务获取数据,我使用如下代码:
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或网站添加泰语时,我会得到正确的泰语文本。唯一的问题是这个邮政编码。
答:
1赞
koen
4/21/2017
#1
您需要按如下方式编写 &<>,就像在 html 文件中一样:
& < >
2赞
Mujtaba
5/11/2019
#2
您可以替换字符串,以便它允许您发送特殊字符。您可以使用字符串方法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]];
此代码还将支持泰语。
评论