我正在使用NSXMLParser来解析Mountain Lion中的消息的聊天记录。该文件的扩展名为.ichat ...
这是我的代码
- (void) doParse:(NSData *)data {
NSString *dString;
UtilClass *Debug = [[UtilClass alloc] init];
/*
NSString *dictionaryString = [data description];
dString = dictionaryString;
[Debug debugLog:dString];
*/
dString = @"xmlparser: 10 \n";
[Debug debugLog:dString];
// create and init NSXMLParser object
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:data];
// create and init our delegate
XMLParser *parser = [[XMLParser alloc] initXMLParser];
// set delegate
[nsXmlParser setDelegate:parser];
// parsing...
BOOL success = [nsXmlParser parse];
// test the result
if (success) {
dString = @"xmlparser: YES \n";
[Debug debugLog:dString];
// get array of users here
// NSMutableArray *users = [parser users];
} else {
NSError *error2 = [nsXmlParser parserError];
dString = @"xmlparser: ERROR :: ";
dString = [dString stringByAppendingFormat:@"%@\n",error2];
[Debug debugLog:dString];
dString = @"xmlparser: NO \n";
[Debug debugLog:dString];
}
[parser release];
[nsXmlParser release];
}
以下是创建NSData并将其发送到上述方法的方法
NSString *logPath = @"~/Library/Messages/Archive/2012-09-06/anything on 2012-09-06 at 10.45.ichat";
NSData *testData = [[[NSData alloc] initWithContentsOfFile:[logPath stringByExpandingTildeInPath]]autorelease];
[self doParse:testData];
最后,我总是收到'NSXMLParserErrorDomain 4',这意味着解析器被赋予一个空文档。 但是,我在解析器传递t0之前记录了'data'变量,数据似乎很好。 这可能是什么问题?
XML文档的前几行
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>$archiver</key>
<string>NSKeyedArchiver</string>
<key>$objects</key>
<array>
<string>$null</string>
<dict>