可能是一个简单的问题,但我加载文件并不是那么好......
在处理XML文档时,我正试图加载XML文档并在控制台中显示其节点的内容。这是我的代码:
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *doc = [mainBundle pathForResource:@"helloworld" ofType:@"musicxml"];
[RootViewController testDoc:doc];
return YES;
}
// Implementation of testDoc: method
+ (void)testDoc:(NSString *)filePath {
NSError *err = nil;
DDXMLDocument *document = [[DDXMLDocument alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:filePath]] options:0 error:&err];
NSLog(@"filePath is %@", filePath);
NSLog(@"document is %@", document);
DDXMLNode *node = [document rootElement];
NSMutableString *xmlContent = nil;
while ((node = [node nextNode])) {
[xmlContent appendFormat:@"%@ ", [node stringValue]];
}
[xmlContent appendString:@"\n"];
NSLog(@"Test data is %@", xmlContent);
}
在控制台中,我看到路径格式正确;在Finder中访问该位置确实显示了我正在寻找的文件。问题是document和xmlContent都打印到(null)。我不太确定装载不足的地方......有人能指出我正确的方向吗?
答案 0 :(得分:0)
我不确定如果在NSLog中将DDXMLDocument打印为%@,但显示xml结构时会发生什么
NSLog(@"%@", [document XMLStringWithOptions:DDXMLNodePrettyPrint]);
要加载文档,请尝试:
DDXMLDocument *document = [[DDXMLDocument alloc] initWithData:[NSData dataWithContentsOfFile:filePath] options:0 error:&err];
xmlContent打印出来为null,因为您没有分配NSMutableString的实例。 尝试设置
NSMutableString *xmlContent = [[[NSMutableString alloc] init] autorelease];