Hi, I want to use the NSXMLParser class to parse through XML data. To help me, I created a TreeNode class that contains information about each node's parent node, child nodes, name and attribute values.
I came up with a solution, but my concern is that any app that uses the code will take a little too much RAM.
Here's my code:
How can I make it better?
I came up with a solution, but my concern is that any app that uses the code will take a little too much RAM.
Here's my code:
Code:
NSMutableArray *unendedElements;
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
// This element is a child of the node that is represented by currentTreeNode.
TreeNode *newTreeNode = [[TreeNode alloc] initWithName:elementName];
[unendedElements addObject:newTreeNode];
NSInteger unendedElementCount = [unendedElements count];
if (unendedElementCount > 1) {
TreeNode *parentNode = [unendedElements objectAtIndex:(unendedElementCount - 2)];
[parentNode addChildNode:newTreeNode];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
[unendedElements removeLastObject];
}
How can I make it better?