Building a Parse Tree

Recipe 13-6 introduces the XMLParser class. Its job is to build a parse tree as the NSXMLParser class works its way through the XML source. The three standard NSXML routines (start element, finish element, and found characters) read the XML stream and perform a recursive depth-first descent through the tree.

The class adds new nodes when reaching new elements (parser:didStartElement: qualifiedName:attributes:) and adds leaf values when encountering text (parser:foundCharacters:). Because XML allows siblings at the same tree depth, this code uses a stack to keep track of the current path to the tree root. Siblings always pop back to the same parent in parser:didEndElement:, so they are added at the proper level.

After finishing ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.