0

I asked a question yesterday about a little app that I would like to make for OSX with Cocoa/Obj-C (Xcode).

The princip is easy:
Got an XML file like this one:

<?xml version="1.0" ?>
<Report>
    <Date>20110311</Date>
    <Title>The Title</Title>
    <Description>Description sample<Description>
</Report>

When the app is opened, there is a window with three text fields.
When a file is loaded (File -> Open) the three text fields get the value of what is in the XML elements.
For my example it will be:

TextFields 1 => 20110311
TextFields 2 => The Title
TextFields 3 => Description sample

And that's it!
As you can see in my description, it's maybe easy...
But I tried a lot of things, I didn't successed :/
I'm a newbie in Cocoa developpment and there's a lot of dedicated things that I don't understand like how can I make links between CODE & GUI...

Now here is my demand:
If someone could make me an Xcode project similar to my example above, for see what's wrong with my different trys, or explain me how this app can be done (with codes examples)...
I've spend 4 long days on that project but didn't have results :(
Help... :(
Miskia

Nono
  • 241
  • 5
  • 15

3 Answers3

6

Two little things to consider:

You should definitely read into the Objective-C / Cocoa documentation.
Otherwise you will never be able to program something yourself.
In the end this will save you lots of time struggling with problems like these.
The things you ask are just the very basics of programming.
(Besides the XML part)

You should never ask people to write complete applications.
People on Stack Overflow are more then willing to help with specific problems,
but they do not work for you.

That being said, here my actual answer:

The XML code you provided contains a syntax error:

The second <Description> should be </Description>

For my example code below I used the following XML file:

<?xml version="1.0" ?>
<Report>
    <Date>20110311</Date>
    <Title>The Title</Title>
    <Description>Description sample</Description>
</Report>

This quickly written example does everything you need.
Create a new application with XCode and open the .....AppDelegate.m file.
Simply paste the Objective-C code mentioned below within the following function:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Paste Here
}

Next click 'Click build and Run'.
Select your file within the NSOpenPanel and click the open-button.
Now you should see a simple dialog with the results.
Hopefully this helps you understand how to parse the XML file.
I can imagine 4 days of struggling must be unpleasant :)

The Objective-C sample code:

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];

}

Here some additional information: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/XMLParsing/Articles/UsingParser.html

Anne
  • 26,765
  • 9
  • 65
  • 71
  • Sigh. It's times like this I'm *desperately* begging Apple to add something as awesome as [LINQ](http://en.wikipedia.org/wiki/Language_Integrated_Query) (..to XML) to Cocoa! – Jay Mar 25 '13 at 08:02
3

Sample project:
http://dd5.org/static/XML.zip

How to create a project like above:
- Create new project
- Add code to the header (.h) and implementation (.m) files
- Design the main window
- Finally add the correct bindings

See images below how to add the bindings (already done in the sample project).
Right-click on the App Delegate drag to the NSTextFied (Picture 1).
Release the button and select the correct entry in the ray menu (Picture 2).

enter image description here enter image description here

Anne
  • 26,765
  • 9
  • 65
  • 71
0

Feel free to look at two easy XML parser classes I created.

http://www.memention.com/blog/2009/10/31/The-XML-Runner.html

Source with projects are available at github

https://github.com/epatel/EPXMLParsers

epatel
  • 45,805
  • 17
  • 110
  • 144