Cocoa XML reader app
Posted
by
Miskia
on Stack Overflow
See other posts from Stack Overflow
or by Miskia
Published on 2011-03-10T15:15:14Z
Indexed on
2011/03/11
0:10 UTC
Read the original article
Hit count: 320
Hello,
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 setup 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
So. The user open the XMLFile, and XMLDocument analyse the file to extract nodes' data and send it to the differents NSTextField... But it doesn't work :(
If someone can help me... I'm a newbie so don't be too rude if I made big mistakes :)
Miskia.
© Stack Overflow or respective owner