Cocoa/Objective-C:解析 XML 文档的最佳实践? [英] Cocoa/Objective-C : Best Practice to parse XML Document?

查看:30
本文介绍了Cocoa/Objective-C:解析 XML 文档的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未在 Cocoa 中使用过 XML,所以我不知道从哪里开始.现在我需要将 XML 文件(来自硬盘)解析为一个对象甚至一个对象数组.

我的 XML 看起来像这样

<名字><姓氏>等等...</人><人物>...

在我的项目中,我已经有一个具有必需属性的 Person 类.从此类 XML 文件创建对象的最佳做法是什么?

解决方案

希望这会有所帮助.

请看:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/XMLParsing/Articles/UsingParser.html#//apple_ref/doc/uid/20002264-BCIIJEEH>

打开文件:

- (void)openXMLFile {NSArray *fileTypes = [NSArray arrayWithObject:@"xml"];NSOpenPanel *oPanel = [NSOpenPanel openPanel];NSString *startingDir = [[NSUserDefaults standardUserDefaults] objectForKey:@"StartingDirectory"];如果 (!startingDir)起始目录 = NSHomeDirectory();[oPanel setAllowsMultipleSelection:NO];[oPanel beginSheetForDirectory:startingDir file:nil types:fileTypesmodalForWindow:[自身窗口] modalDelegate:selfdidEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:)上下文信息:无];}- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {NSString *pathToFile = nil;如果(返回代码 == NSOKButton){pathToFile = [[[工作表文件名] objectAtIndex:0] copy];}如果(路径到文件){NSString *startingDir = [pathToFile stringByDeletingLastPathComponent];[[NSUserDefaults standardUserDefaults] setObject:startingDir forKey:@"StartingDirectory"];[self parseXMLFile:pathToFile];}}

解析:

- (void)parseXMLFile:(NSString *)pathToFile {布尔成功;NSURL *xmlURL = [NSURL fileURLWithPath:pathToFile];if (addressParser)//addressParser 是一个 NSXMLParser 实例变量[addressParser 发布];addressParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];[addressParser setDelegate:self];[addressParser setShouldResolveExternalEntities:YES];成功 = [addressParser 解析];//未使用的返回值//如果不成功,则通知委托有错误}

I never worked with XML in Cocoa before so I have no idea where to begin. Right now I need to parse a XML File (from Hard disc) into an object or even an Array of objects.

My XML looks like this

<Person>
   <FirstName>
   <LastName>
   etc...
</Person>
<Person>
...

In my project I already have a Person class with a required properties. What is the best practice to create objects from such XML file?

解决方案

Hope this helps.

Please take a look at:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/XMLParsing/Articles/UsingParser.html#//apple_ref/doc/uid/20002264-BCIIJEEH

Opening File:

- (void)openXMLFile {
    NSArray *fileTypes = [NSArray arrayWithObject:@"xml"];
    NSOpenPanel *oPanel = [NSOpenPanel openPanel];

    NSString *startingDir = [[NSUserDefaults standardUserDefaults] objectForKey:@"StartingDirectory"];
    if (!startingDir)
        startingDir = NSHomeDirectory();

    [oPanel setAllowsMultipleSelection:NO];
    [oPanel beginSheetForDirectory:startingDir file:nil types:fileTypes
      modalForWindow:[self window] modalDelegate:self
      didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:)
      contextInfo:nil];
}

- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {
    NSString *pathToFile = nil;
    if (returnCode == NSOKButton) {
        pathToFile = [[[sheet filenames] objectAtIndex:0] copy];
    }

    if (pathToFile) {
        NSString *startingDir = [pathToFile stringByDeletingLastPathComponent];
        [[NSUserDefaults standardUserDefaults] setObject:startingDir forKey:@"StartingDirectory"];

        [self parseXMLFile:pathToFile];
    }
}

Parse:

- (void)parseXMLFile:(NSString *)pathToFile {
    BOOL success;

    NSURL *xmlURL = [NSURL fileURLWithPath:pathToFile];

    if (addressParser) // addressParser is an NSXMLParser instance variable
        [addressParser release];

    addressParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    [addressParser setDelegate:self];
    [addressParser setShouldResolveExternalEntities:YES];

    success = [addressParser parse]; // return value not used
                // if not successful, delegate is informed of error
}

这篇关于Cocoa/Objective-C:解析 XML 文档的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆