objective-c - Cocoa/Obj-C 简单 XML 文件阅读器 - 需要帮助

标签 objective-c xml cocoa xcode

我昨天问了一个关于我想使用 Cocoa/Obj-C (Xcode) 为 OSX 制作的小应用程序的问题。

原理很简单:
得到一个像这样的 XML 文件:

<?xml version="1.0" ?>
<Report>
    <Date>20110311</Date>
    <Title>The Title</Title>
    <Description>Description sample<Description>
</Report>

打开应用程序时,会出现一个包含三个文本字段的窗口。
加载文件时(文件 -> 打开),三个文本字段将获取 XML 元素中的值。
对于我的示例,它将是:

TextFields 1 => 20110311
TextFields 2 => 标题
TextFields 3 => 说明示例

就是这样!
正如您在我的描述中看到的,这可能很容易......
但是我尝试了很多东西,但没有成功:/
我是 Cocoa 开发的新手,有很多我不明白的专门的东西,比如如何在 CODE 和 GUI 之间建立链接......

现在这是我的要求:
如果有人可以为我制作一个类似于上面示例的 Xcode 项目,看看我的不同尝试有什么问题,或者向我解释一下这个应用程序是如何完成的(带有代码示例)...
我在那个项目上花了 4 天时间,但没有结果:(
帮助...:(
米斯基亚

最佳答案

需要考虑的两件小事:

您绝对应该阅读 Objective-C/Cocoa 文档。
否则你将永远无法自己编程。
最后,这将为您节省大量时间来解决此类问题。
您问的问题只是编程的基础知识。
(除了 XML 部分)

您永远不应该要求人们编写完整的应用程序。
Stack Overflow 上的人们更愿意帮助解决特定问题,
但它们不适合你。

话虽这么说,这是我的实际答案:

您提供的 XML 代码包含语法错误:

The second <Description> should be </Description>

对于下面的示例代码,我使用了以下 XML 文件:

<?xml version="1.0" ?>
<Report>
    <Date>20110311</Date>
    <Title>The Title</Title>
    <Description>Description sample</Description>
</Report>

这个快速编写的示例可以满足您所需的一切。
使用 XCode 创建一个新应用程序并打开 .....AppDelegate.m 文件。
只需将下面提到的 Objective-C 代码粘贴到以下函数中即可:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Paste Here
}

接下来单击“单击构建并运行”。
在 NSOpenPanel 中选择您的文件并单击打开按钮。
现在您应该看到一个包含结果的简单对话框。
希望这可以帮助您了解如何解析 XML 文件。
我可以想象4天的挣扎一定是不愉快的:)

Objective-C 示例代码:

NSOpenPanel *openPanel  = [NSOpenPanel openPanel];
NSArray *fileTypes = [NSArray arrayWithObjects:@"xml",nil];

NSInteger result  = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes ];

if(result == NSOKButton){

    NSString * input =  [openPanel filename];

    NSXMLDocument* doc = [[NSXMLDocument alloc] initWithContentsOfURL: [NSURL fileURLWithPath:input] options:0 error:NULL];

    NSMutableArray* dates = [[NSMutableArray alloc] initWithCapacity:10];
    NSMutableArray* titles = [[NSMutableArray alloc] initWithCapacity:10];
    NSMutableArray* descriptions = [[NSMutableArray alloc] initWithCapacity:10];

    NSXMLElement* root  = [doc rootElement];

    NSArray* dateArray = [root nodesForXPath:@"//Date" error:nil];
    for(NSXMLElement* xmlElement in dateArray)
        [dates addObject:[xmlElement stringValue]];

    NSArray* titleArray = [root nodesForXPath:@"//Title" error:nil];
    for(NSXMLElement* xmlElement in titleArray)
        [titles addObject:[xmlElement stringValue]];

    NSArray* descriptionArray = [root nodesForXPath:@"//Description" error:nil];
    for(NSXMLElement* xmlElement in descriptionArray)
        [descriptions addObject:[xmlElement stringValue]];

    NSString * date = [dates objectAtIndex:0];
    NSString * title = [titles objectAtIndex:0];
    NSString * description = [descriptions objectAtIndex:0];

    NSString *output = [NSString stringWithFormat:@"Date:  %@\nTitle:  %@\nDescription:  %@", date, title, description];
    NSRunAlertPanel( @"Result", output, @"OK", nil, nil );

    [doc release];
    [dates release];
    [titles release];
    [descriptions release];

}

这里有一些附加信息: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/XMLParsing/Articles/UsingParser.html

关于objective-c - Cocoa/Obj-C 简单 XML 文件阅读器 - 需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5274513/

相关文章:

ios - UITextView 禁用文本选择

java - 将自定义 META 标记添加到 XML 文件

objective-c - 在 cocoa 中的常量文件变量中设置颜色

objective-c - 如何在 Cocoa 中移动 Sprite ?

objective-c - 如何检测 OSX 上正在运行什么应用程序

ios - 将完成处理程序保存为对象

iphone - iOS : How to Preload Data before Display

javascript - 如何使用 xsl 值作为 onclick 的 ID

ios - 按下导航 Controller 左栏按钮时如何停止 View 消失

php - 如何在 rss feed 的 <description> 标签中显示链接