当前位置: 动力学知识库 > 问答 > 编程问答 >

Read local xml file in app iOS

问题描述:

In the bundle I have a connection to an external folder that is blue color and not the usual yellow color.

Inside this folder there is an xml file from which I have to read the content.

This is the xml file from which I derive the value of "id":

 <?xml version='1.0' encoding='UTF-8'?>

<root>

<event id="2"></event>

</root>

This is my code:

- (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);

}

}

网友答案:

Blue folders in Xcode appear as actual folders in your app's bundle. This means you need to include the folder name in the file's path in your code:

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *folderPath = [bundlePath stringByAppendingPathComponent:@"folderName"];
NSString *path = [folderPath stringByAppendingPathComponent:@"config.xml"];

Replace folderName with the actual name of your folder.

Or you can do:

NSURL *xmlURL = [[NSBundle mainBundle] URLForResource:@"config" withExtension:@"xml" subdirectory:@"folderName"];
网友答案:

Try this one :

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

    NSDictionary *settingsDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] 
    pathForResource:configFileURL ofType:@"xml"]];
分享给朋友:
您可能感兴趣的文章:
随机阅读: