Cocoa XML 阅读器应用程序

发布于 2024-10-21 03:36:11 字数 2814 浏览 1 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘年祭陌 2024-10-28 03:36:11

你并没有太具体地说明什么不起作用,但这里对一些可能会引起麻烦的事情有一两个猜测...

作为一个新的 Cocoa 用户很容易忘记的一件事是 IBOutlets 必须实际上是连接到 Interface Builder。按住 Control 键并从每个文本字段拖动到 Document.xib 中的文件所有者,并确保将它们分配到正确的出口。

另一个看起来很奇怪的项目(尽管我不确定它会导致问题)是使用 for 循环来设置文本字段的 stringValue 。如果每个元素的数组中有多个项目,则您需要在设置文本字段的值之前自行连接字符串。重复设置该值只会删除以前的值。如果每个数组中只有一项,为什么不简单地这样做:

NSArray *dateElement = [root 
               nodesForXPath:@"//Report/ReportCreationDate" 
                       error:nil];
[dateField setStringValue:
           [[dateElement objectAtIndex:0] stringValue]];

最后,您可能想尝试确保在查询 NSXMLDocument 时没有错误:

NSError *dateErr;
NSArray *dateElement = [root 
               nodesForXPath:@"//Report/ReportCreationDate" 
                       error:&dateErr];
if( dateElement ){
    // set the stringValue
}
else {
    // inspect the error
}

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 the stringValue 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:

NSArray *dateElement = [root 
               nodesForXPath:@"//Report/ReportCreationDate" 
                       error:nil];
[dateField setStringValue:
           [[dateElement objectAtIndex:0] stringValue]];

Finally, you might want to try making sure there are no errors when you're querying the NSXMLDocument:

NSError *dateErr;
NSArray *dateElement = [root 
               nodesForXPath:@"//Report/ReportCreationDate" 
                       error:&dateErr];
if( dateElement ){
    // set the stringValue
}
else {
    // inspect the error
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文