开发者

Loading XML document from bundle through KissXML (iOS)

Probably a simple question, but I'm not that good with loading files…

Getting my feet wet with processing XML documents, I'm attempting to load an XML document and display the contents of its nodes in the console. Here's my code:

// 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(@"开发者_JAVA技巧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);
}

In the Console, I see that the path is formatted properly; going to that location in Finder does reveal the file I'm looking for. The issue is that both document and xmlContent print out to (null). I'm not quite sure where the lack of loading is taking place…can anyone point me in the right direction?


I'm not sure what happens if you print the DDXMLDocument as a %@ in NSLog, but to display the xml structure do a

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

For loading the document try:

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

xmlContent prints out to null because you didn't assign an instance of NSMutableString. Try setting

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜