XML を解析するための簡単なテスト プロジェクトを作成しました。このテスト用にアプリで .plist ファイルを作成しました。これを documentsDirectory にコピーします。ファイルがコピーされていることがわかります。NSXMLParser の NSData オブジェクトに初期化しようとすると、次のようになります。
The operation couldn’t be completed. (NSXMLParserErrorDomain error 4.)
これは、ファイルが存在しないことを示しています。NSData が nil ではないこと、および fileExistsAtPath が正常であることを確認しました。私は次に何をしようか迷っています。何かご意見は?ありがとう。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createEditableCopyOfDatabaseIfNeeded];
NSString *path = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"testList.plist"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
if (data == nil) {
NSLog(@"yes nil");
}
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)createEditableCopyOfDatabaseIfNeeded
{
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *writableDBPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"testList.plist"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"testList.plist"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
- (NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
#pragma mark - NSXMLParserDelegate
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"hg19Packet0.bin"]) {
NSLog(@"first");
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSLog(@"%@", [parseError localizedDescription]);
}