Objective c Cocoa/Obj-C简单XML文件读取器-需要帮助吗
昨天我问了一个问题,我想用Cocoa/Obj-C(Xcode)为OSX制作一个小应用程序 原则很简单:Objective c Cocoa/Obj-C简单XML文件读取器-需要帮助吗,objective-c,xml,cocoa,xcode,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> &l
获取了如下XML文件:
<?xml version="1.0" ?>
<Report>
<Date>20110311</Date>
<Title>The Title</Title>
<Description>Description sample<Description>
</Report>
20110311
标题
描述样本
当应用程序打开时,会出现一个包含三个文本字段的窗口。
加载文件时(file->Open),三个文本字段获取XML元素中的值
以我为例,它将是: TextFields 1=>20110311
TextFields 2=>标题
TextFields 3=>描述样本 就这样!
正如你在我的描述中看到的,这可能很容易。。。
但我尝试了很多东西,但都没有成功:/
我是Cocoa开发的新手,有很多我不了解的事情,比如如何在代码和GUI之间建立链接。。。
现在我的要求是:
如果有人能让我做一个类似于我上面例子的Xcode项目,看看我的不同尝试有什么问题,或者向我解释这个应用程序是如何实现的(用代码例子)。。。
我花了4天的时间在这个项目上,但没有结果:(
救命啊(
米斯基亚需要考虑两件小事: 您应该仔细阅读Objective-C/Cocoa文档。
否则,您将无法自己编程。
最后,这将为您节省大量的时间来解决此类问题。
您所要求的只是编程的基础。
(除了XML部分) 您永远不应该要求他人编写完整的应用程序。
堆栈溢出的人更愿意帮助解决特定问题,
但它们不适合你 话虽如此,我的实际答案是: 您提供的XML代码包含语法错误:
The second <Description> should be </Description>
下一步单击“单击构建并运行”。在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];
}
以下是一些附加信息:
示例项目:如何创建如上所述的项目:
-创建新项目
-向头文件(.h)和实现文件(.m)添加代码
-设计主窗口
-最后添加正确的绑定
请参见下面的图片,了解如何添加绑定(已在示例项目中完成)。
右键单击应用程序代理并将其拖动到NSTextFied(图1)。
松开按钮并在“光线”菜单中选择正确的条目(图2)
请随意查看我创建的两个简单的XML解析器类 github上提供了项目的源代码
叹气。在这种情况下,我拼命恳求苹果为可可添加一些像(…到XML)一样棒的东西!
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Paste Here
}
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];
}