Parsing XML in Cocoa

Here’s how it works:

There’s a class called NSXMLParser. It’s used to parse XML files. However, NSXMLParser is stupid. All it knows how to do is parse XML, but it doesn’t know what it’s supposed to do with the information it finds.

Enter a delegate. A delegate is like a nanny. Since the XMLParser doesn’t have a clue what to do with the information it finds, it goes and asks its delegate about each and every thing: “Hey! I started parsing a document! Am I supposed to do anything?” “Hey! I found some CDATA! What am I supposed to do with it!” “Hey! I found another tag!” “Hey! I found a closing tag!”, and so on. All of these “Hey!” statements are delegate methods, or in other words, they are optional methods that a delegate object may choose to implement. Usually (but not always), the object that creates the NSXMLParser is also the delegate, but that doesn’t have to be the case.

So you might have something like this:

NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:someURLToAnXMLFile];
[parser setDelegate:self];
[parser parse];
[parser release];

Then in that same object (self), you might have some of these methods:

- (void)parserDidStartDocument:(NSXMLParser *)parser {
  //the parser started this document. what are you going to do?
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
  //the parser found an XML tag and is giving you some information about it
  //what are you going to do?
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  //the parser found some characters inbetween an opening and closing tag
  //what are you going to do?
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
  //the parser finished. what are you going to do?
}

There are a whole bunch of these methods listed in the documentation. Simply go to the NSXMLParser class reference, and they’re all listed under the “Delegate Methods” section. Once you get the hang of it, NSXMLParser is pretty easy to use. It is a SAX Parser, which means it’s event-driven parser. It finds stuff, and it tells you about it.

Leave a Comment