gpt4 book ai didi

xml - 通过 KissXML (iOS) 从包中加载 XML 文档

转载 作者:行者123 更新时间:2023-11-28 17:46:49 24 4
gpt4 key购买 nike

可能是个简单的问题,但我不太擅长加载文件......

开始尝试处理 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 中的那个位置确实显示了我正在寻找的文件。问题是文档和 xmlContent 都打印到 (null)。我不太确定加载不足发生在何处……谁能指出正确的方向?

最佳答案

我不确定如果在 NSLog 中将 DDXMLDocument 打印为 %@ 会发生什么,但要显示 xml 结构,请执行以下操作

NSLog(@"%@", [document XMLStringWithOptions:DDXMLNodePrettyPrint]);

要加载文档,请尝试:

DDXMLDocument *document = [[DDXMLDocument alloc] initWithData:[NSData dataWithContentsOfFile:filePath] options:0 error:&err];

xmlContent 输出为空,因为您没有分配 NSMutableString 的实例。尝试设置

NSMutableString *xmlContent = [[[NSMutableString alloc] init] autorelease];

关于xml - 通过 KissXML (iOS) 从包中加载 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5363024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com