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

Non*_*ono 0 xml xcode cocoa objective-c

昨天我问了一个问题,我想用Cocoa/Obj-C(Xcode)为OSX做一个小应用程序.

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

<?xml version="1.0" ?>
<Report>
    <Date>20110311</Date>
    <Title>The Title</Title>
    <Description>Description sample<Description>
</Report>
Run Code Online (Sandbox Code Playgroud)

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

TextFields 1 => 20110311
TextFields 2 => 标题
TextFields 3 => 描述示例

就是这样!
正如你在我的描述中所看到的那样,它可能很容易......
但是我尝试了很多东西,我没有成功:/
我是Cocoa开发中的新手,还有很多我不懂的专用内容就像我如何在CODE和GUI之间建立链接...

现在这是我的要求:
如果有人可以让我成为一个类似于上面例子的Xcode项目,看看我的不同尝试有什么问题,或者解释我这个应用程序是如何的完成(用代码示例)...
我在该项目上度过了4天,但没有结果:(
帮助...... :(
Miskia)

Ann*_*nne 6

需要考虑的两件小事:

您一定要阅读Objective-C/Cocoa文档.
否则你将永远无法自己编程.
最后,这将为您节省大量时间来解决这些问题.
你问的东西只是编程的基础.
(除了XML部分)

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

话虽如此,这里是我的实际答案:

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

The second <Description> should be </Description>
Run Code Online (Sandbox Code Playgroud)

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

<?xml version="1.0" ?>
<Report>
    <Date>20110311</Date>
    <Title>The Title</Title>
    <Description>Description sample</Description>
</Report>
Run Code Online (Sandbox Code Playgroud)

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

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Paste Here
}
Run Code Online (Sandbox Code Playgroud)

然后单击"单击构建并运行".
在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];

}
Run Code Online (Sandbox Code Playgroud)

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