访问目标中的本地文件路径C

我已经添加了一个文件夹名称资源,在它下面有另一个文件夹名文件文件夹里面有两个xml文件,我试图解析。当我给我的资源文件夹pathforResource方法名称显示路径为空。我如何从这些文件夹访问这两个XML文件? Resources-> File-> text.xml。 我的代码是这样的,访问目标中的本地文件路径C

NSMutableArray *aryXMLName = [NSMutableArray new]; 

[aryXMLName addObject:@"text.xml"];

[aryXMLName addObject:@"test1.xml"];

for(NSString *str in aryXMLName){

NSString *path = [[NSBundle mainBundle] pathForResource:str ofType:@"Resources"];

NSLog(@"Path : %@",path);

NSData *data=[NSData dataWithContentsOfFile:path];

if ([str isEqualToString:@"text.xml"]) {

if (data == nil)

return;

NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"string: %@", str);

NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:str];

NSLog(@"dictionary: %@", xmlDoc);

}else if ([str isEqualToString:@"test1.xml"]){

if (data == nil)

return;

NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"string: %@", str);

NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:str];

NSLog(@"dictionary: %@", xmlDoc);

}

}

的情况是这样的,

enter image description here

回答:

替换此代码:

 NSMutableArray *aryXMLName = [NSMutableArray new]; 

[aryXMLName addObject:@"test"];

[aryXMLName addObject:@"text1"];

for(NSString *str in aryXMLName){

NSString *path = [[NSBundle mainBundle] pathForResource:str ofType:@"xml"];

NSLog(@"Path : %@",path);

NSData *data=[NSData dataWithContentsOfFile:path];

if ([str isEqualToString:@"test"]) {

if (data == nil)

return;

NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"string: %@", str);

NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:str];

NSLog(@"dictionary: %@", xmlDoc);

}else if ([str isEqualToString:@"text1"]){

if (data == nil)

return;

NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"string: %@", str);

NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:str];

NSLog(@"dictionary: %@", xmlDoc);

}

}

以上是 访问目标中的本地文件路径C 的全部内容, 来源链接: utcz.com/qa/261460.html

回到顶部