iPhone,字符串来自utf8 xml,字符错误

时间:2012-04-15 09:48:16

标签: iphone ios xml utf-8 rss

我必须显示包含在我从网上获取的xml文件中的文本。该文件以:

开头

<?xml version =“1.0”encoding =“utf-8”?> ...

然后有文本节点,写成如下:

< text><![CDATA [E ancora,il fascino dellecittÃdel nord,che con la primavera conquistano ore luce,o di isole dove laPasquaèunrito rimasto fedele neltempo.Pasquaèun 'casualione pe ...]]>< / text>

这是非常好的utf-8格式文本吗?

我已经尝试了所有这些,但仍然不能用“à”代替“Ô,用“è”代替“Ô:

text = ^the string from above^
NSData* data_text = [text dataUsingEncoding:NSUTF8StringEncoding];
NSString* text_c3 = [[NSString alloc] initWithBytes:[data_text bytes] length:[data_text length] encoding:NSUTF8StringEncoding];

const char* utfstring = "E ancora, il fascino delle città del nord,";// [text UTF8String];
NSString* text_c1 = [[NSString alloc] initWithUTF8String:utfstring];
NSString* text_converted = [NSString stringWithCString:utfstring encoding:NSUTF8StringEncoding];

有什么建议吗? 感谢。

3 个答案:

答案 0 :(得分:0)

无法从您的问题中判断CDATA部分是否采用UTF-8编码。由于编码数据是二进制数据,您需要发布二进制数据(即以十六进制表示)而不是一些文本。

此外,您的所有代码示例都取决于使用正确编码存储的源代码文件,这也无法从您的问题中确定。

我建议您使用一个不错的XML解析器来解析XML数据,例如内置的 NSXMLParser 类。遇到CDATA部分时,无论原始编码是什么,它都会调用您的委托并将文本作为UTF-8编码的二进制数据传递。然后,您可以将其转换为字符串:

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)cdataBlock
{
    NSString *text = [NSString stringWithUTF8String: [cdataBlock bytes]];

    // do something with the text...
}

确保将XML文件检索为二进制数据,并将其作为XML数据传递给XML解析器。如果没有,您将有不必要的解码和编码,并且您可能会损坏编码。

答案 1 :(得分:0)

NSURL * url = [NSURL URLWithString:@“http://www.yourURL.com”];

//NSString *jsonRequest = [NSString stringWithFormat:@"&json_data=%@",[[NSString stringWithFormat:@"{\"createInvoice\":{\"client_id\":\"%@\",\"address\":\"\",\"invoice_title\":\"%@\",\"number\":\"\",\"date\":\"%@\",\"due_date\":\"%@\",\"late_fee\":\"\",\"schedule_date\":\"%@\",\"po_number\":\"\",\"status\":\"paid\",\"notes\":\"invoice notes\",\"terms\":\"invoice terms\",\"items\":[{\"name\":\"Test4\",\"type\":\"Product\",\"description\":\"\",\"unit_cost\":\"15\",\"quantity\":\"100\",\"discount\":\"10\",\"discount_type\":\"Percent\",\"tax1_name\":\"\",\"tax1_type\":\"\",\"tax2_name\":\"\",\"tax2_type\":\"\",\"tax1_percent\":\"\",\"tax2_percent\":\"\"}]}}",selected_Client_ID,invoiceTitleTxtFld.text,invoiveDateTxtFld.text,invoiceDueDateTxtFld.text,scheduleOnTxtFld.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];



NSString *jsonRequest = [NSString stringWithFormat:@"&json_data=%@",[[NSString stringWithFormat:@"{\"updateInvoice\":{\"invoice_id\":\"%@\",\"client_id\":\"60105\",\"address\":\"ABS\",\"invoice_title\": \"%@\",\"number\":\"inv-7774 new\",\"date\":\"2012-03-30\",\"due_date\":\"2012-04-30\",\"late_fee\":\"\",\"schedule_date\":\"2012-03-31\",\"po_number\":\"PO-7770\",\"status\":\"paid\",\"notes\":\"invoice notes\",\"terms\":\"invoice terms\",\"items\":[{\"name\":\"Test4\",\"type\":\"Product\",\"description\":\"Test4 desc\",\"unit_cost\":\"15\",\"quantity\":\"100\",\"discount\":\"10\",\"discount_type\":\"Percent\",\"tax1_name\":\"\",\"tax1_type\":\"\",\"tax2_name\":\"\",\"tax2_type\":\"\",\"tax1_percent\":\"\",\"tax2_percent\":\"\"}]}}",delegate.invoice_ID,invoiceTitleTxtFld.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];


NSData *json_data = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];


[request setHTTPMethod:@"POST"];
[request setHTTPBody: json_data];
NSLog(@"%@",json_data);
//[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [json_data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[[jsonRequest stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
                      dataUsingEncoding:NSUTF8StringEncoding 
                      allowLossyConversion:YES]];

//  [NSURLConnection connectionWithRequest:[request autorelease] delegate:self];

NSUrlsConnection * conn_InvEdit = [[NSURLConnection alloc]                                       initWithRequest:要求                                       委托:自我];

// Successful connection.
if (conn_InvEdit) {

    // [self initSpinner];
    // [self spinBegin];

    NSMutableData *data = [[NSMutableData alloc] init];
    self.receivedData=data;
    [data release];
} 
// Unsuccessful connection.
else {

}  
// Clean up
[url release];
[request release];
// Close keypad.

Try with this code, If you still have any problem then send me your complete question at  akram.khan@mail.vinove.com. I'll send you sample app with solution.

答案 2 :(得分:0)

...

添加像这样的标记

...

并使用

[request setValue:@“application / x-www-form-urlencoded”forHTTPHeaderField:@“Content-Type”];

和[请求setValue:[NSString stringWithFormat:@“%d”,[json_data length]] forHTTPHeaderField:@“Content-Length”]; [request setHTTPBody:[[jsonRequest stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]                       dataUsingEncoding:NSUTF8StringEncoding                       allowLossyConversion:YES]];