views:

848

answers:

1

Hello:

I am parsing an XML file for two elements: "title" and "noType". Once these are parsed, I am adding them to an object called aMaster, an instance of my own Master class that contains NSString variables.

I am then adding these instances to an NSMutableArray on a singleton, in order to call them elsewhere in the program. The problem is that when I call them, they don't seem to be on the same NSMutableArray index... each index contains either the title OR the noType element, when it should be both... can anyone see what I may be doing wrong? Below is the code for the parser. Thanks so much!!

#import "XMLParser.h"
#import "Values.h"
#import "Listing.h"
#import "Master.h"

@implementation XMLParser

@synthesize sharedSingleton, aMaster;

- (XMLParser *) initXMLParser {

    [super init];
    sharedSingleton = [Values sharedValues];
    aMaster = [[Master init] alloc];
    return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    aMaster = [[Master alloc] init];

     //Extract the attribute here.

    if ([elementName isEqualToString:@"intro"]) {
     aMaster.intro = [attributeDict objectForKey:@"enabled"];
    } else if ([elementName isEqualToString:@"item"]) {
     aMaster.item_type = [attributeDict objectForKey:@"type"];
     //NSLog(@"Did find item with type %@", [attributeDict objectForKey:@"type"]);

     //NSLog(@"Reading id value :%@", aMaster.item_type);
    } else {
     //NSLog(@"No known elements");
    }

    //NSLog(@"Processing Element: %@", elementName);    //HERE
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

    if(!currentElementValue) 
     currentElementValue = [[NSMutableString alloc] initWithString:string];
    else {
     [currentElementValue appendString:string];//[tempString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
     CFStringTrimWhitespace((CFMutableStringRef)currentElementValue);
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"item"]) {
     [sharedSingleton.master addObject:aMaster];
     NSLog(@"Added %@ and %@ to the shared singleton", aMaster.title, aMaster.noType);  //Only having one at a time added... don't know why
     [aMaster release];

     aMaster = nil;

    } else if ([elementName isEqualToString:@"title"]) {
     [aMaster setValue:currentElementValue forKey:@"title"];
    } else if ([elementName isEqualToString:@"noType"]) {
     [aMaster setValue:currentElementValue forKey:@"noType"]; 
     //NSLog(@"%@ should load into the singleton", aMaster.noType);
    }
    NSLog(@"delimiter");
    NSLog(@"%@ should load into the singleton", aMaster.title);
    NSLog(@"%@ should load into the singleton", aMaster.noType);

    [currentElementValue release];
    currentElementValue = nil;

}

- (void) dealloc {
    [aMaster release];
    [currentElementValue release];
    [super dealloc];
}

@end
A: 

Every time that didStartElement is called, you're setting aMaster to a new instance of the Master class. Based on the implementation of didEndElement, it looks like you should only be creating a new instance whenever a new item tag is found. This could be why each entry in the array has one value or the other, since a new instance is created for each value.

Andy
Thank you Andy! That was the culprit. I had completely missed that.
IF that answers your question, you should Accept my answer (press the checkmark on the left side of my answer), so that it doesn't show up as unanswered anymore.
Andy