Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
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:

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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.