2012-04-15 62 views
0

我要顯示包含到一個XML文件,我從網上獲取文本。該文件的開頭爲:?iPhone,從UTF8 XML字符串錯誤的字符

< XML版本= 「1.0」 編碼= 「UTF-8」>

...再有就是文本節點,這樣寫的:?

<文本> <![CDATA [然而,北部城市,與彈簧征服小時光照,或島嶼,復活節是在儀式仍然tempo.Pasqua忠實的魅力是PE的機會... ]]> < /文本>

這是很好的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]; 

有什麼建議? 感謝。

+0

在xml中有沒有à或Ã? – 2012-04-15 10:21:23

+0

'utfstring'不是UTF8String字符串,它只是一個8位字符串,可能是內部'kCFStringEncodingMacRoman'格式,它的編碼範圍爲129-255。在unicode中,這些字符將由多個(本例中爲兩個)8位代碼組成, – zaph 2012-04-15 10:44:06

回答

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數據。如果沒有,你將有不必要的decodings和編碼,你很可能會損壞編碼。

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的頁頭] 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 [email protected] I'll send you sample app with solution. 
0

...

添加標籤這樣

...

並使用

[請求的setValue:@ 「應用程序/ x WWW的形式進行了urlencoded」 forHTTPHeaderField:@ 「內容類型」]; HTTP請求setValue:[NSString stringWithFormat:@「%d」,[json_data length]] forHTTPHeaderField:@「Content-Length」]; [請求setHTTPBody:[[jsonRequest stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding] dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];