iosnsdictionarytbxml

Loading NSDictionary parsing xml file


I've been stuck a little on this problem. Here is the situation : I'm parsing an xml file with positions, I parse it well, thing is I want to put the different elements into an NSDictionary (one NSDictionary for each position) and those NSDictionary into an NSMutableArray.

- (void) traverseElement:(TBXMLElement *)element {

NSMutableDictionary *position = [[NSMutableDictionary alloc]init];
NSArray *keys = [NSArray arrayWithObjects:@"cab",@"idagencedepart", @"idagencefinale",@"iddestinataire",@"idexpediteur",@"idtransporteurreexpedition",@"departement",@"message1", nil];
[position dictionaryWithValuesForKeys:keys];

do {

    TBXMLAttribute * attribute = element->firstAttribute;


    // if attribute is valid

    while (attribute) {


        if ([[TBXML elementName:element] isEqualToString:@"referencecolis"]) {
            [position setObject:[TBXML textForElement:element] forKey:@"cab"];
            NSLog(@"cab : %@",[TBXML textForElement:element]);
            };

        if ([[TBXML elementName:element] isEqualToString:@"idagencedepart"]) {
            [position setObject:[TBXML textForElement:element] forKey:@"idagencedepart"];
            NSLog(@"idagencedepart : %@",[TBXML textForElement:element]);

        };

        [modulectrlcle addObject:position];

  attribute = attribute->next;



    }

    if (element->firstChild)

        [self traverseElement:element->firstChild];



} while ((element = element->nextSibling));

}

}

Here is my code. It parses well, but my NSMutableArray (modulectrle) is filled with weird NSDictionaries...


Solution

  • Allocate NSDictonary inside while loop:

    do {
         TBXMLAttribute * attribute = element->firstAttribute;
         while (attribute) {
            NSMutableDictionary *position = [[NSMutableDictionary alloc]init];
    
            BOOL foundElement = NO;
            if ([[TBXML elementName:element] isEqualToString:@"referencecolis"]) {
                [position setObject:[TBXML textForElement:element] forKey:@"cab"];
                NSLog(@"cab : %@",[TBXML textForElement:element]);
                foundElement = YES;
            };
    
            if ([[TBXML elementName:element] isEqualToString:@"idagencedepart"]) {
                [position setObject:[TBXML textForElement:element] forKey:@"idagencedepart"];
                NSLog(@"idagencedepart : %@",[TBXML textForElement:element]);
                foundElement = YES;
            };
    
            if(foundElement) //avoid adding empty NSDictonaries
            {
                 [modulectrlcle addObject:position];
            }
    
            attribute = attribute->next;
        }
    
        if (element->firstChild)
            [self traverseElement:element->firstChild];
    
    } while ((element = element->nextSibling));
    

    Otherwise you add same NSDictonary all over again to an array and you push all elements into same dictionary and as a result you get array with a same dictionary several times.