我正在使用 NSXMLParser 来解析 Mountain Lion 中 Messages 的聊天日志。该文件的扩展名为 .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 解析器之前记录了“数据”变量,并且数据看起来很好。这里可能是什么问题?
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>