2013-08-29 4 views
2

번들에 파란색으로되어 있고 일반적인 노란색이 아닌 외부 폴더에 연결되어 있습니다. 이 폴더 안에는 내용을 읽어야하는 xml 파일이 있습니다.응용 프로그램의 로컬 XML 파일 읽기 iOS

<?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

'[파서 구문 분석]''당신이 인쇄 할 수 있습니다에 실패는 [파서는 parserError]'에 대한 정보를 얻을 수있는 경우 실패. –

+0

오류가 발생합니다. (NSXMLParserErrorDomain error 5.) –

답변

4

블루 폴더 앱의 실제 폴더로 표시

내가 "ID"의 값을 도출되는 XML 파일입니다 묶음.

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

는 시도이 하나

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

    NSDictionary *settingsDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] 
    pathForResource:configFileURL ofType:@"xml"]];