Cocoa XML 阅读器应用程序
我是Cocoa的新手,只是在Windows上用C/C++开发一些小应用程序。 我想在 Cocoa 上制作一个“简单”的应用程序。 当用户特定的 XML 文件时,文件节点表示为“最终用户可查看”。
我用一些 NSTextField 做了一个界面。 我创建了一个名为“XMLFile”的 NSDocument 子类,因此我在 Xcode 项目中得到了“XMLFile.h”和“XMLFile.m”。
在我的应用程序的 plist 中,我设置了一个新的“文档类型”: XML 文件 - 扩展名:xml - 角色:视图 - 类:XMLFile - 存储类型:XML
这是我的“XMLFile.h”:
#import <Cocoa/Cocoa.h>
@interface FichierXML : NSDocument {
}
IBOutlet NSTextField *dateField;
IBOutlet NSTextField *titleField;
IBOutlet NSTextField *descField;
IBOutlet NSTextField *vidfileField;
IBOutlet NSTextField *imgfileField;
IBOutlet NSObjectController *object;
NSUInteger *mask;
@end
这是我的“XMLFile.m”:
#import "XMLFile.h"
@implementation XMLFile
- (BOOL)readFromData:(NSData *)datafile ofType:(NSString *)typeName error:(NSError **)outerror
{
NSMutableArray* ReportCreationDate = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray* ReportTitle = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray* ReportDescription = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray* VideoPath = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray* VideoThumbnailImageName = [[NSMutableArray alloc] initWithCapacity:10];
NSXMLDocument* doc = [[NSXMLDocument alloc] initWithData:datafile options:mask error:outerror];
NSXMLElement* root = [doc rootElement];
NSArray* dateElement = [root nodesForXPath:@"//Report/ReportCreationDate" error:nil];
for(NSXMLElement* xmlElement in dateElement)
[dateElement setStringValue:[xmlElement stringValue]];
NSArray* titleElement = [root nodesForXPath:@"//Report/ReportTitle" error:nil];
for(NSXMLElement* xmlElement in titleElement)
[titleField setStringValue:[xmlElement stringValue]];
NSArray* descElement = [root nodesForXPath:@"//Report/ReportDescription" error:nil];
for(NSXMLElement* xmlElement in descElement)
[descField setStringValue:[xmlElement stringValue]];
NSArray* vidfileElement = [root nodesForXPath:@"//Report/Videos/Video/VideoPath" error:nil];
for(NSXMLElement* xmlElement in vidfileElement)
[vidfileField setStringValue:[xmlElement stringValue]];
NSArray* imgfileElement = [root nodesForXPath:@"//Report/Videos/Video/VideoThumbnailImageName" error:nil];
for(NSXMLElement* xmlElement in imgfileElement)
[imgfileField setStringValue:[xmlElement stringValue]];
[doc release];
[ReportCreationDate release];
[ReportTitle release];
[ReportDescription release];
[VideoPath release];
[VideoThumbnailImageName release];
return YES;
}
@end
用户打开 XMLFile,XMLDocument 分析该文件以提取节点的数据并将其发送到不同的 NSTextField。但这不起作用。
如果有人可以帮助我。
I'm a newbie to Cocoa, just develop some little apps with C/C++ on Windows.
I want to make a "simple" app on Cocoa.
When the user specific XML file, the file nodes are represented "enduser viewable".
I made an interface with some NSTextField.
I made a subclass of NSDocument called "XMLFile" so I got "XMLFile.h" and "XMLFile.m" in my Xcode project.
In the plist of my app I set up a new "Document Types":
XML File - extensions: xml - role: view - class: XMLFile - store type: XML
Here is my "XMLFile.h":
#import <Cocoa/Cocoa.h>
@interface FichierXML : NSDocument {
}
IBOutlet NSTextField *dateField;
IBOutlet NSTextField *titleField;
IBOutlet NSTextField *descField;
IBOutlet NSTextField *vidfileField;
IBOutlet NSTextField *imgfileField;
IBOutlet NSObjectController *object;
NSUInteger *mask;
@end
And here is my "XMLFile.m":
#import "XMLFile.h"
@implementation XMLFile
- (BOOL)readFromData:(NSData *)datafile ofType:(NSString *)typeName error:(NSError **)outerror
{
NSMutableArray* ReportCreationDate = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray* ReportTitle = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray* ReportDescription = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray* VideoPath = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray* VideoThumbnailImageName = [[NSMutableArray alloc] initWithCapacity:10];
NSXMLDocument* doc = [[NSXMLDocument alloc] initWithData:datafile options:mask error:outerror];
NSXMLElement* root = [doc rootElement];
NSArray* dateElement = [root nodesForXPath:@"//Report/ReportCreationDate" error:nil];
for(NSXMLElement* xmlElement in dateElement)
[dateElement setStringValue:[xmlElement stringValue]];
NSArray* titleElement = [root nodesForXPath:@"//Report/ReportTitle" error:nil];
for(NSXMLElement* xmlElement in titleElement)
[titleField setStringValue:[xmlElement stringValue]];
NSArray* descElement = [root nodesForXPath:@"//Report/ReportDescription" error:nil];
for(NSXMLElement* xmlElement in descElement)
[descField setStringValue:[xmlElement stringValue]];
NSArray* vidfileElement = [root nodesForXPath:@"//Report/Videos/Video/VideoPath" error:nil];
for(NSXMLElement* xmlElement in vidfileElement)
[vidfileField setStringValue:[xmlElement stringValue]];
NSArray* imgfileElement = [root nodesForXPath:@"//Report/Videos/Video/VideoThumbnailImageName" error:nil];
for(NSXMLElement* xmlElement in imgfileElement)
[imgfileField setStringValue:[xmlElement stringValue]];
[doc release];
[ReportCreationDate release];
[ReportTitle release];
[ReportDescription release];
[VideoPath release];
[VideoThumbnailImageName release];
return YES;
}
@end
The user opens the XMLFile, and XMLDocument analyses the file to extract nodes' data and sends it to the differents NSTextField. But it doesn't work.
If someone can help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你并没有太具体地说明什么不起作用,但这里对一些可能会引起麻烦的事情有一两个猜测...
作为一个新的 Cocoa 用户很容易忘记的一件事是 IBOutlets 必须实际上是连接到 Interface Builder。按住 Control 键并从每个文本字段拖动到 Document.xib 中的文件所有者,并确保将它们分配到正确的出口。
另一个看起来很奇怪的项目(尽管我不确定它会导致问题)是使用
for
循环来设置文本字段的stringValue
。如果每个元素的数组中有多个项目,则您需要在设置文本字段的值之前自行连接字符串。重复设置该值只会删除以前的值。如果每个数组中只有一项,为什么不简单地这样做:最后,您可能想尝试确保在查询 NSXMLDocument 时没有错误:
You're not too specific about what doesn't work, but here's a guess or two at some things that could be causing trouble...
One thing that can be easy to forget as a new Cocoa user is that IBOutlets have to be actually hooked up in Interface Builder. Control-drag from each text field to File's Owner in your Document.xib, and make sure that they are assigned to the correct outlet.
Another item that seems strange (though I don't know for sure it would cause a problem) is your use of a
for
loop to set thestringValue
of the text fields. If there is more than one item in each element's array, you will need to concatenate the strings yourself before setting the text field's value. Repeatedly setting the value will simply get rid of the previous value. If there is only one item in each array, why not simply do:Finally, you might want to try making sure there are no errors when you're querying the NSXMLDocument: