Search code examples
nsarraynsdictionarynsxmlparser

parse XML to NSArray of NSDictionary - error - why?


I have a problem parsing a XML to NSArray of NSDictionary. I don't know why, but instead of the array gets 2 objects (in this case), it take 2 objects with equal data... Why?

Here's the code:

@interface RLparseXMLToArrayOfDictionarys : NSObject <NSXMLParserDelegate> {
    NSMutableArray *arrayWithResult;
    NSMutableDictionary *tempDict;
    NSMutableString *currentString;
    NSString *groupKey;
}

@property (nonatomic, strong) NSString *groupKey;
@property (nonatomic, retain) NSMutableArray *arrayWithResult;
@property (nonatomic, retain) NSMutableDictionary *tempDict;

-(NSArray *)parseXMLWithStringToArray:(NSString *)stringWithXML withGroupKey:(NSString *)groupKeyToIgnore;

@end




@implementation RLparseXMLToArrayOfDictionarys

@synthesize groupKey;
@synthesize arrayWithResult;
@synthesize tempDict;


- (id)init
{
    self = [super init];
    if (self) {

    }

    return self;
}


-(NSArray *)parseXMLWithStringToArray:(NSString *)stringWithXML withGroupKey:(NSString *)groupKeyToIgnore{
    NSData *currentStringData = [stringWithXML dataUsingEncoding:NSUTF8StringEncoding];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:currentStringData];
    [parser setDelegate:self];

    // Set Parser Options
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];

    //key to ignore
    self.groupKey = groupKeyToIgnore;

    if (!arrayWithResult) {
        arrayWithResult = [[NSMutableArray alloc] init];
    }

    if (!tempDict) {
        tempDict = [[NSMutableDictionary alloc] init];
    }

    [parser parse];

    NSLog(@"return: %@", arrayWithResult);

    return arrayWithResult;
}


#pragma mark -
#pragma mark XML methods

- (void)parserDidStartDocument:(NSXMLParser *)parser
{

}


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

}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if(!currentString){
        currentString = [[NSMutableString alloc] init];
    }

    [currentString appendString:string];
}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    NSString *currentStringNoWhiteSpace = [currentString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if ([elementName isEqualToString:groupKey]){
        [arrayWithResult addObject:tempDict];
//      [tempDict removeObjectsForKeys:[tempDict allKeys]];
    }

    else if (currentStringNoWhiteSpace != nil)
        [tempDict setValue:currentStringNoWhiteSpace forKey:elementName];

    currentStringNoWhiteSpace = nil;
    currentString = nil;
}


- (void)parserDidEndDocument:(NSXMLParser *)parser {

}

@end

Solution

  • Done!

    Replace [tempDict removeObjectsForKeys:[tempDict allKeys]]; with tempDict = [[NSMutableDictionary alloc] init];

    :)