我是Iphone开发新手,我在本地保存了一个xml文件,并使用NSXmlParser(存储在Resource文件夹中)显示详细信息。但是,现在我想从url(从客户端)下载新的xml,并且需要将新的xml文件存储在Cache目录中,并且还要从资源文件夹中删除xml文件。但是我没有解析缓存目录中的新xml文件,如何从缓存目录中解析新的xml文件?任何人都知道这一点,请帮帮我
答案 0 :(得分:0)
我实际上刚刚为我的一个项目编写了这个功能。我将分享以前用来保存XML文件并将其写入缓存的代码。
- (NSString *)XMLCacheFilePath
{
NSString *directory = @"/Library/Caches/XML/";
NSString *fileName = [@"file" stringByAppendingPathExtension:@"xml"];
// Setup directory path
NSString *homeDirectoryPath = NSHomeDirectory();
NSString *unexpandedDirectoryPath = [homeDirectoryPath stringByAppendingString:directory];
NSString *directoryPath = [NSString pathWithComponents:[NSArray arrayWithObjects:
[NSString stringWithString:[unexpandedDirectoryPath stringByExpandingTildeInPath]],
nil]];
// Setup file path
NSString *unexpandedFilePath = [directoryPath stringByAppendingPathComponent:fileName];
NSString *filePath = [NSString pathWithComponents:[NSArray arrayWithObjects:
[NSString stringWithString:[unexpandedFilePath stringByExpandingTildeInPath]],
nil]];
if (![[NSFileManager defaultManager] fileExistsAtPath:directoryPath isDirectory:nil]) {
NSError *error = [[NSError alloc] init];
[[NSFileManager defaultManager] createDirectoryAtPath:directoryPath
withIntermediateDirectories:YES
attributes:nil
error:&error];
}
return filePath;
}
- (void)saveXMLStringToDisk:(NSString *)xmlString
{
NSError *error;
[xmlString writeToFile:self.XMLCacheFilePath
atomically:YES
encoding:NSUTF8StringEncoding
error:&error];
}
- (NSString *)XMLStringFromDisk
{
NSString *XMLString = [NSString stringWithContentsOfFile:self.XMLCacheFilePath
encoding:NSUTF8StringEncoding
error:nil];
return XMLString;
}