我创建了一个简单的测试项目来解析XML。我在我的应用程序中为这个测试创建了一个.plist文件。我把它复制到documentsDirectory上。我可以看到文件被复制过来了。当我尝试将它初始化为NSXMLParser的NSData对象时,我得到:
The operation couldn’t be completed. (NSXMLParserErrorDomain error 4.)这告诉我这个文件不存在。我检查了NSData是否为空,以及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]);
}发布于 2012-05-15 06:30:51
你从文本编辑器看过你的样例plist了吗?如果你将它保存为一个二进制属性列表,它将不能使用NSXMLParser解析(我相信)。但是,您可以使用NSPropertyListSerialization来解析它。
https://stackoverflow.com/questions/10591324
复制相似问题