如何在目标 C 中使用 XML 解析发布带有特殊字符和泰语的字符串?

How to post string with special character and Thai Language using XML parsing in Objective C?

提问人:Muju 提问时间:4/21/2017 最后编辑:halferMuju 更新时间:5/11/2019 访问量:1311

问:

我是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.)" 

如何发布具有特殊字符的数据。

我正在用泰语添加类似的东西

enter image description here

但我得到这个:

enter image description here

与印地语相同

enter image description here

输出是这样的

enter image description here

但是,当有人使用Android或网站添加泰语时,我会得到正确的泰语文本。唯一的问题是这个邮政编码。

iOS Objective-C XML 解析

评论

0赞 koen 4/21/2017
你的特殊性格是什么,你能在有和没有它的情况下共享(部分)你的xml文件吗?错误代码 111 可能表示格式错误的 xml 文件:stackoverflow.com/questions/20454853/nsxmlparsererrordomain-111
0赞 Muju 4/21/2017
@Koen 我正在添加特殊字符“&<>”。
0赞 koen 4/21/2017
请参阅下面的回答。
0赞 Muju 4/21/2017
@Koen 不,我直接需要按原样传递它。因为我还需要用泰语传递它。我不想替换字符串。
1赞 koen 4/21/2017
请阅读此内容,也许会有所帮助:stackoverflow.com/questions/1091945/...

答:

1赞 koen 4/21/2017 #1

您需要按如下方式编写 &<>,就像在 html 文件中一样:

&amp; &lt; &gt;
2赞 Mujtaba 5/11/2019 #2

您可以替换字符串,以便它允许您发送特殊字符。您可以使用字符串方法stringByReplacingOccurrencesOfString

 NSString *RaisedBy="Your Special character string";
        RaisedBy = [RaisedBy stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"];
        RaisedBy = [RaisedBy stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];
        RaisedBy = [RaisedBy stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];
        RaisedBy = [RaisedBy stringByReplacingOccurrencesOfString:@"'" withString:@"&apos;"];
        RaisedBy = [RaisedBy stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

此代码还将支持泰语。