如何从文档目录中获取xml文件列表

时间:2012-04-03 06:02:50

标签: iphone xml

我正在开发一个应用程序。我想从文档目录中获取所有.xml文件的名称,所以请告诉我如何执行此操作。

2 个答案:

答案 0 :(得分:2)

试试这个:

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *xml_path = [documentsDir stringByAppendingPathComponent:@"xmlfolder"]; // if you have folder 
NSArray *list =[NSBundle pathsForResourcesOfType:@"xml" inDirectory:xml_path];
NSLog(@"%@",list);

答案 1 :(得分:0)

请执行以下操作:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [paths objectAtIndex:0];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:docPath error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.xml'"];
NSArray *xmlFiles = [dirContents filteredArrayUsingPredicate:fltr];