Web Services XML or JSON

Most web services are exposed via RESTful interfaces such as XML or JSON.

XML
XML looks like this. Basically it starts off with the version number, and then a dictionary or array within. This dictionary or array can contain even more dictionaries and arrays or any other objects. The next thing we have to do is to parse through this message.


Steve Paul Have a good weekend!

XML Parsers
There are two types of XML parsers, the DOM and SAX parsers. The DOM notifies you when it runs through the tree. A SAX parser reads and builds up an in-memory representation of the XML. Both has its advantages and disadvantages.

Many to choose from (C / Obj-C)
NSXML, 
libxml2
TBXML
TouchXML
KissXML
TinyXML
GDataXML

Advantages/drawbacks
Validation
XPath

For the parser itself, we have many to choose from. The included parsers are NSXMLParser and libxml2 parser. The rest are created by third-party developers and have proven to work. Each parser, again has its own advantages / disadvantages to watch out for, such as validation of each step as it parses through the XML, and also XPath which functions as an index.

NSXMLParser
Let’s run through the included XML parser, NSXMLParser. The first thing you have to do is to create the parser, set the delegate to self, and any properties you may wish to set. During the creation of the XMLParser, be sure to init with whatever XML you have.

Create parser/set delegate & properties

NSXMLParser *parser = [[NSXMLParser alloc] initWithURL:myURL];
parser.delegate = self;
parser.shouldProcessNameSpaces = NO;

Parse

[parser parse];

Receive from delegate methods

 – (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
– (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName 
 – (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

JSON
Another form of RESTful interface is JSON, which is Javascript Object Notation. Here’s how the same message looks like in JSON. This is more human-readable, according to many.
To parse JSON, we can use a third-party JSON framework, called SBJSON. SBJSON was created by Stig Brautaset. It was originally named JSON-framework but renamed soon after to SBJSON.

SBJSON
To use SBJSON, we simply download, drag and copy into our own project. If you are using XCode 4, you might want to check out how to link projects instead of copying them over. The instructions are a bit more complicated, but are also found in the same SBJSON link.
The next thing we need to do is to import JSON.h, create the SBJSON parser, and then parse it into an NSDictionary. If we do not know what the final object is, we can simply use the id type. Download, drag and drop, copy into folder.

Import/create/parse

#import “JSON.h”
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *object = [parser objectWithString:json_string error:nil];