iPhone - 如何在Documents目录中下载xml文件

时间:2011-04-18 14:28:32

标签: xml parsing download documents

我要解析一个xml文件,但首先我必须下载并存储在我的/ Documents目录中。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

-(NSString *)downloadXML:(NSString *)path newFileName:(NSString *)newFileName {

    //NSLog(@"\nENTRATO NEL METODO DI DOWNLOAD");
    NSURL *URL = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    NSData *xmlData = [NSData dataWithContentsOfURL:URL];
    if(xmlData == nil) {
        // Error - handle appropriately
    }
    NSString *applicationDocumentsDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *storePath=[applicationDocumentsDir stringByAppendingPathComponent:newFileName];
    [xmlData writeToFile:storePath atomically:TRUE];

    //NSLog(@"\nFINITO IL METODO DI DOWNLOAD");
    return storePath;

}