Saturday, April 26, 2014

Mapping XML to Objects With NSXMLParser

Justin Driscoll:

The XMLObjectMapper class acts as our NSXMLParser delegate. It’s responsible for setting up the parser and responding to events. It also handles accumulating text and maintaining two stacks, one for elements and one for objects. These two stacks are the heart of this solution. When the parser starts a new element the mapper adds that element to the end of the elements stack and then asks its delegate what, if any, object that element maps to. The delegate then returns either a new object or the current object which is pushed onto the end of the objects stack. When an element ends, the object at the end of the stack is passed the text contents of the element (if any) and then both the element and the object are popped off the end.

Comments RSS · Twitter

Leave a Comment