2012-04-15 23 views
0

。 ?ファイルで始まる: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にはàまたはÃがありますか? –

+0

'utfstring'はUTF8String文字列ではなく、ちょうど8ビットの文字列で、内部の' kCFStringEncodingMacRoman'形式のもので、129-255の範囲の文字をエンコードしています。ユニコードでは、これらの文字は複数(この場合は2つ)の8ビットコードで構成され、 – zaph

答えて

0

これは、CDATAの一部が正しくUTF-8でエンコードされたかどうかは、あなたの質問から教えすることはできません。データはバイナリデータを符号化されているので、あなたは、バイナリデータ(すなわち、16進数表記で)としないテキストを投稿する必要があります。

さらに、すべてのコードサンプルは、ソースコードファイルを再度ご質問から判断することはできません正しいエンコーディングで保存されているに依存します。

私はあなたがそのようなビルトインNSXMLParserクラスとしてXMLデータを、解析するためにまともなXMLパーサーを使用して提案しました。それはCDATAセクションに遭遇すると、それはあなたのデリゲートを呼び出しますし、関係なく、元のエンコードが何であったか、UTF-8エンコーディングでバイナリデータとしてテキストを渡しません。あなたは、文字列に変換することができます

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

    // do something with the text... 
} 

そして、あなたがバイナリデータとしてXMLファイルを取得し、XMLデータとしてXMLパーサにそれを渡すことを確認してください。ない場合は、不要なデコーディングとエンコーディングを持って、あなたはエンコーディングを損傷する可能性があります。

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

...

この

のようにタグ付けを追加します...

[要求のsetValue: "アプリケーション/ x-www-form-urlencodedで" forHTTPHeaderField @: "コンテンツタイプ" @]を使用します。

[要求セット値:[NSString stringWithFormat:@ "%d"、[json_data length]] forHTTPHeaderField:@ "Content-Length"]; [[jsonRequest stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding] dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];

関連する問題