在捆绑包中,我连接到一个蓝色的外部文件夹,而不是通常的黄色。 在这个文件夹里面有一个xml文件,我必须从中读取内容。
这是我从中获取“id”值的xml文件:
<?xml version='1.0' encoding='UTF-8'?>
<root>
<event id="2"></event>
</root>
这是我的代码:
- (void)viewDidLoad
{
NSString *pathFile = [[NSBundle mainBundle] bundlePath];
NSString *path = [[NSString alloc] initWithString:[pathFile stringByAppendingPathComponent:@"config.xml"]];
NSURL *xmlURL = [NSURL fileURLWithPath:path];
NSXMLParser *parser = [[ NSXMLParser alloc] initWithContentsOfURL:xmlURL];
NSLog(@"the parser xml is %@", parser);
//the parser xml is <NSXMLParser: 0x967d870>
[parser setDelegate:self];
BOOL success = [parser parse];
if(success == YES){
NSLog(@"success");
} else {
NSLog(@" not success"); //is not success, why?
}
[parser release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
//in this method does not enter
if ([elementName isEqualToString:@"event"]){
NSLog(@" %@", elementName);
}
}
答案 0 :(得分:4)
Xcode中的蓝色文件夹在应用程序包中显示为实际文件夹。这意味着您需要在代码中的文件路径中包含文件夹名称:
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *folderPath = [bundlePath stringByAppendingPathComponent:@"folderName"];
NSString *path = [folderPath stringByAppendingPathComponent:@"config.xml"];
将folderName
替换为文件夹的实际名称。
或者你可以这样做:
NSURL *xmlURL = [[NSBundle mainBundle] URLForResource:@"config" withExtension:@"xml" subdirectory:@"folderName"];
答案 1 :(得分:1)
试试这个:
NSString *configFileURL = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"XMLFileName"];
NSDictionary *settingsDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:configFileURL ofType:@"xml"]];