Cocoa wrapper for expat xml parser

The expat parser is an open source, event driven xml parser written in C. It's the underlying XML parser for the open source Mozilla project, perl's XML::Parser, and other open-source XML parsers.

Since expat is written in C, it may not exactly be the ideal parser for Cocoa developers to use. That's where the Cocoa wrapper comes in. The wrapper is actually a drop in replacement for NSXMLParser. It uses the same API as NSXMLParser. This allows two things

  1. Easy coding, using familiar Objective-C message passing.
  2. Easy replacement. If you already have parsing code for NSXMLParser, you only need to change a few lines of code to try out expat!

API

Note this is the exact API used by NSXMLParser.

- (id)initWithContentsOfURL:(NSURL *)url
- (id)initWithData:(NSData *)data
- (id)delegate
- (void)setDelegate:(id)delegate
- (void)setShouldProcessNamespaces:(BOOL)shouldProcessNamespaces
- (BOOL)shouldProcessNamespaces
- (void)setShouldReportNamespacePrefixes:(BOOL)flag
- (BOOL)shouldReportNamespacePrefixes
- (void)setShouldResolveExternalEntities:(BOOL)shouldResolveExternalEntities
- (BOOL)shouldResolveExternalEntities
- (BOOL)parse
- (void)abortParsing
- (NSError *)parserError
- (int)columnNumber
- (int)lineNumber
- (NSString *)publicID
- (NSString *)systemID

Delegate Methods

- (void)parserDidStartDocument:(id)parser
- (void)parserDidEndDocument:(id)parser
- (void)parser:(id)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
- (void)parser:(id)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
- (void)parser:(id)parser didStartMappingPrefix:(NSString *)prefix toURI:(NSString *)namespaceURI
- (void)parser:(id)parser didEndMappingPrefix:(NSString *)prefix
- (void)parser:(id)parser foundCharacters:(NSString *)string
- (void)parser:(id)parser foundComment:(NSString *)comment
- (void)parser:(id)parser foundProcessingInstructionWithTarget:(NSString *)target data:(NSString *)data
- (void)parser:(id)parser parseErrorOccurred:(NSError *)parseError

You may notice there are a few delegate methods missing. In some cases there wasn't an exact equivalent (that I knew of) in expat, such as parser:foundIgnorableWhitespace. (Does anyone actually use this method anyways? I mean, it's ignorable, right?) But most of the missing delegate methods that are missing have to do with parsing the DTD. I didn't implement these because I don't believe they work in NSXMLParser, and also because I'm lazy and didn't need them for what I was using!

Download

The code comes in the form of an Xcode project folder. To use it in your own project, all you need is the ExpatXMLParser.h and ExpatXMLParser.m files. You also need the expat library. A precompiled version (1.9.5) is included in the project. This was compiled on my machine by downloading the latest release version at the time of this writing and doing a simple configure, make install. You may copy this into your project as well, or use your own version.

Download Now

This code is completely free, so feel free to do whatever you want with it. It was inspired by Rafael R. Sevilla's Expatobjc project.

If you make improvements to this code, I'd love it if you send me the improvements so I can include them for everybody else.
Also, if you find this code useful, or have some comments for me, feel free to let me know.
Either way, you can email me.