Home > Articles > Web Services > XML

Cocoa: Working with XML

  • Print
  • + Share This
Objective-C and Cocoa is the predominate language/framework on the Apple OS X operating system, but it is fairly uncommon otherwise. One way to enable an application written in this language to communicate with applications written for other platforms is via the universal XML. Marcus Zarra walks you through constructing, transmitting, and deconstructing an XML document using Objective-C and Cocoa.
Like this article? We recommend Cocoa XML - Title Page

XML is fast becoming (some would argue that it has already become) the dominant file structure on the Internet. If your application "speaks" XML, it is assumed that it can communicate with any application on any other platform. Although this description is overly broad, the fact remains that to communicate effectively on the Internet your application should be speaking XML. Fortunately, using Cocoa/Objective-C makes this process fairly straightforward.

There are three phases to an XML communication:

  • Constructing an XML document
  • Transmitting (both sending and receiving) an XML document
  • Deconstructing an XML document

This article walks through building an XML document in memory by using Objective-C, transmitting that document to a server on the Internet, and processing its response—also in XML. This potentially complicated workflow is actually rather trivial.

Constructing an XML Document

Support for XML documents is built directly into Cocoa. The primary object for creating an XML document is called NSXMLDocument, and inside this object is a root object of type NSXMLNode. The root NSXMLNode can contain attributes and children that are also NSXMLNodes (or the child class NSXMLElement). The following builds an XML document:

- (NSData *)constructXMLRequest
  NSXMLElement *root = [[NSXMLElement alloc] initWithName:@"Request"];
  [root addAttribute:[NSXMLNode attributeWithName:@"Attribute1" stringValue:@"Value1"]];
  [root addAttribute:[NSXMLNode attributeWithName:@"Attribute2" stringValue:@"Value2"]];
  [root addAttribute:[NSXMLNode attributeWithName:@"Attribute3" stringValue:@"Value3"]];
  NSXMLElement *childElement1 = [[NSXMLElement alloc] initWithName:@"ChildElement1"];
  [root addChild:childElement1];
  [childElement1 release];
  NSXMLElement *childElement2 = [[NSXMLElement alloc] initWithName:@"ChildElement2"];
  [childElement2 addAttribute:[NSXMLNode attributeWithName:@"ChildAttribute2.1" stringValue:@"Value2.1"]];
  [childElement2 setStringValue:@"ChildValue2.1"];
  [root addChild:childElement2];
  [childElement2 release];
  NSXMLDocument *xmlRequest = [NSXMLDocument documentWithRootElement:root];
  [root release];
  NSLog(@"XML Document\n%@", xmlRequest);
  return [xmlRequest XMLData];

In this example I constructed a root node that is an NSXMLElement. I then added attributes to that element that are NSXMLNode objects. I also added children to the root element of both NSXMLElement objects and NSXMLNode objects.

The output from the NSLog statement is as follows:

<Request Attribute1="Value1" Attribute2="Value2" Attribute3="Value3">
  <ChildElement2 ChildAttribute2.1="Value2.1">ChildValue2.1</ChildElement2>

At the end of this method I returned the XMLDocument as an NSData object that is ready to be transmitted to the server.

  • + Share This
  • 🔖 Save To Your Account