2012-07-17 76 views
0

我已經使這種方法從URL下載XML。 問題是編碼字符。XML下載NSUTF8String編碼問題

-(void) scaricamentoXML{ 
    /*Save XML IN LOCALE*/ 
    NSString* docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
    NSLog(@"Doc dir: %@:",docsDir); 
    //dichiaro il file locale in cui salvare i dati XML 
    NSString* fileToDownload = @"provaXML.xml"; 

    NSString *hostURLString = @"http://demo.gigamips.com/rss.php?authkey=12345"; 
    NSURL *xmlURL = [NSURL URLWithString:hostURLString]; 
    NSLog(@"xmlURL = %@", xmlURL); 
    //gestione codifica UTF-8 
    NSError *error; 
    NSString *dataString = [[NSString alloc] initWithContentsOfURL:xmlURL encoding:NSASCIIStringEncoding error:&error]; 
    NSLog(@"DataString = %@", dataString); 
    //NSData *xml = [[NSData alloc]initWithContentsOfURL:xmlURL]; 
    NSData *xml = [dataString dataUsingEncoding:NSUTF8StringEncoding]; 
    //creo una stringa di appoggio in encoding UTF8 - verificare se funziona 
    NSString *str = [[NSString alloc] initWithData:xml encoding:NSUTF8StringEncoding]; 
    NSLog(@"NSData xml = %@", xml); 
    //NSString *newStr = [[NSString alloc] initWithData:xml encoding:NSUTF8StringEncoding]; 
    //NSLog(@"newStr = %@", newStr); 
    NSString *filePath = [docsDir stringByAppendingPathComponent: fileToDownload]; 

    //[xml writeToFile: filePath atomically: NO]; 
    [str writeToFile: filePath atomically: NO]; 
    /*Save XML IN LOCALE*/ 


    /*SALVO LE IMMAGINI MINI IN LOCALE*/ 
    AuthParserImages *parser=[[AuthParserImages alloc]init]; 
    entryArray=[parser startParserLocale:@"provaXML"]; 
    GestioneImages *gi = [[GestioneImages alloc]init]; 
    for(int i =0; i< [entryArray count];i++){ 
     item=[entryArray objectAtIndex:i]; 
     [gi saveImageLocal:item.urlImageLow :item.id]; 
    } 
    /*SALVO LE IMMAGINI MINI IN LOCALE*/ 


    /*CREA IL FILE GESTIONE NEW*/ 
    gp = [[GestionePlist alloc]init]; 
    [gp savePlist:@"begin"]; 
    /*CREA IL FILE GESTIONE NEW*/ 

    [self controlloXML]; 
    [activityIndicator stopAnimating]; 

} 

我看不到的特殊卡拉科特像'或重點ù - A - ò等....

這是Web服務http://demo.gigamips.com/rss.php?authkey=12345

回答

0

問題是在你的Web服務。您沒有收到以UTF8字符串編碼編碼的數據。

+0

是否存在用於檢查此URL是否爲XML的網站UTF- 8實時? – WalterFantauzzi 2012-07-17 08:02:18

0

儘管http://demo.gigamips.com/rss.php?authkey=12345中的XML是以UTF-8編碼的,但它不使用正確的字符。例如,捲髮單引號'(U + 2019)應該出現的地方,它使用U + 0092,這是一個不可打印的控制字符。

它看起來像文檔的作者寫的Windows-1252,然後使用一個工具,以爲這是latin-1它轉換爲UTF-8。

您可以嘗試說服作者首先使用正確的字符,或者編寫代碼來替換可能的替代字符(即U + 2018而不是U + 0091,U + 2019而不是U + 0092,U + 201C而不是U + 0093,依此類推......您可以使用Windows-1252的字符表作爲參考)