What is the pros/cons of NSXMLParser & JSON parser?
Which one is good in which scenario?
Currently, my app uses NSXMLParser. I'm planning to move JSON parser if it is more efficient.
NSXMLParser is an "event driven" parser which basically notifies a delegate about the occurrence of certain elements in the XML document.
Event driven parsers do not create a representation of the XML document by itself. The actual processing of the elements has to be done by some delegate. Properly utilizing event driven parsers is elaborate and error prone and requires experience how to approach such a task. Well, you know it.
NSJSONSerialization on the other hand, and all other third party JSON parsers that I know of, create a foundation object (a
NSDictionary) from the JSON input. Parsing a JSON document and getting a
NSDictionary or a
NSArray object back is a matter of one statement. A few also support the "event driven" mode.
XML is far more complex than JSON. Inherently, a JSON parser is much more simpler and also almost always more efficient in parsing documents.
Despite it's simplicity, JSON is almost always sufficient to express your data.
So, when you can express your data in JSON, by any means, use JSON. If possible, use
Other third party JSON parsers may offer additionally features, like an event driven API, an improved way to handle chunks of data, have more sophisticated options to customize certain edge cases, like the handling of Unicode NULL character, Unicode noncharacters, how to convert JSON numbers, etc., and may be possibly faster than
NSJSONSerialization is about as fast as JSONKit. (For some input, JSONKit is a bit faster). AFAIK, there are two third party parsers which are for any input almost always faster than
NSJSONSerialization, especially on arm, and when it comes to convert Numbers. You can expect them to be faster for a factor in the range of 1 to 2. But consider parsing JSON is almost never the culprit for performance issues.