2

在捆绑包中,我连接到一个蓝色而不是通常的黄色的外部文件夹。在这个文件夹中有一个 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);

    }

}
4

2 回答 2

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"];
于 2013-08-29T16:24:47.160 回答
1

试试这个:

    NSString *configFileURL = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"XMLFileName"];

    NSDictionary *settingsDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] 
    pathForResource:configFileURL ofType:@"xml"]];
于 2014-07-02T09:21:28.590 回答