Geek Speak: Random Mac Stuff You Need to Know
http://www.macfanatic.net/blog/
Copyright © 2007 Matt Brewer
Using NSXML
This month I’m going to take a little time and provide a sample Xcode project detailing the basics of using the Cocoa classes that comprise all of NSXML. There is a powerful XML parser provided in Cocoa as well other classes to help create and modify the XML data. For simplicity sake, I won’t be going into parsing XML documents today. We’ll be focusing on setting up a basic app that lets the user enter some data and then write that out to a XML file.
If you aren’t familiar with XML programming, there are a few well written documents available to help you learn about the material. I would suggest this article from ADC as a good starting point. This is the same stuff that’s in the Xcode documentation as well.
Creating our App: Overview
I’m going to assume that you have a decent working knowledge of Cocoa basics. I make very basic use of the bindings system and that’s about it. You need to be a little familiar with Interface Builder to figure out how everything is connected. There is a complete functioning project that you can download from here.
The quick application that we are going to create today doesn’t serve a masterful purpose. As an overview, we have an object acting as our application controller (AppController in the project) which has an array of Person objects, as well as an action that’s hooked up to the “Create XML” button in the window.
We are going to let the user create/remove Person objects by interacting with the insert/remove methods of the NSArrayController and let the bindings do their magic. Essentially, the user will create a new Person object and presumably edit the default values of first and last name as well as edit a string for comments. That’s it. When the user presses the “Create XML” button, our -(IBAction)createXMLDoc:(id)sender is called, creating the NSXMLDocument with all the children in the right spot.
The Code
Let’s dive into some code. First, below is the simple code for the -()createNSXMLElement. There is a in depth explanation in the comments in the project, but basically we are going to send this message to each Person object when we want the Person object to be represented as something like:
<Person>
<First Name> some Value </First Name>
<Last Name> some Value </Last Name>
<Comments> some Value </Comments>
</Person>
-(NSXMLElement *)createXMLElement {
// Create a root element and name it with our class name
NSXMLElement *element = [[NSXMLElement alloc] initWithName:@"Person"];
// Create NSXMLElements for each property in this Person Object.
// Add those as children
[element addChild:
[NSXMLNode elementWithName:@"First Name" stringValue:firstName]];
[element addChild: [NSXMLNode elementWithName:@"Last Name" stringValue:lastName]];
[element addChild: [NSXMLNode elementWithName:@"Comments" stringValue:comments]];
return element;
}
Now we can call something like the code below to have a representation of the Person object: NSXMLElement *e = [personObject createXMLElement];
We just need to hook up our “Create XML” button to some useful code to generate the full document. We create an array of NSXMLElements by sending the selector to each
Person object. Then we create a root node and create an NSXMLDocument from that.
Writing the document to a file is as simple as [xmlData writeToFile:someFilename];
// Create NSXMLElements from Person Objects and add to xmlElementArray
NSEnumerator *e = [peopleArray objectEnumerator];
NSMutableArray *xmlElementArray = [NSMutableArray new];
Person *object;
while ( object = [e nextObject] ) {
[xmlElementArray addObject:[object createXMLElement]];
}
// Create root of tree. This node has all the children we created in the
//xmlElementArray
NSXMLElement *root = [NSXMLNode elementWithName:@"People Array" children:xmlElementArray attributes:nil];
// Create the doc itself (with Children from xmlElementArray)
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithRootElement:root];
[xmlDoc setVersion:@"1.0"];
// Just some formal stuff here
[xmlDoc setCharacterEncoding:@"UTF-8"];
Conclusion
This wasn’t meant to be a comprehensive or in-depth look at the NSXML classes provided in Cocoa. However, it is a nice introduction to what you can accomplish if you sit down and work through your problem. I would suggest entering some input and then looking at the resulting XML file in a text editor, just to see what is going on in there. Tweak some code and learn!
There are certainly some other great examples available on the net, so be sure to check around. Download the full project (link above) to view more documentation and a working example. Email with questions if you have them.
Contact Info
You can always send me an email at mbrewer@maccompanion.com or visit my website at http://www.macfanatic.net/blog/ for more information about me and my ramblings. I also produce a weekly audio podcast taking an in-depth review of cool and new Mac applications, along with tips, developer interviews, sample code, and the occasional tutorial.