views:

280

answers:

2

Hi! I have followed this tutorial and the parsing part is working allright. My problem is that when printing out the values that I store into the object (in my case the object is a car) always remains null. I save all the objects into an MutableArray which always seems to have a length/count == 0...what am I doing wrong? The array is, as can be seen in the code, defined in the delegate class...

- (XMLParser *) initXMLParser {
    [super init];
    appDelegate = (Car2GoAppDelegate *) [[UIApplication sharedApplication]delegate];
    currentElementValue = [[NSMutableString alloc]init];
    return self;
}


- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attribute:(NSDictionary *)attributeDict {   
    if([elementName isEqualToString:@"kml"]) {
     appDelegate.cars = [[NSMutableArray alloc]init];
    }
    else if([elementName isEqualToString:@"Placemark"]) {
     aCar = [[Car alloc]init];
    }
    else
    return;
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *) string {
    [currentElementValue appendString:string];

}


- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *) elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if([elementName isEqualToString:@"kml"])
     return;
    if([elementName isEqualToString:@"Style"])
     return;
    if([elementName isEqualToString:@"IconStyle"])
     return;
    if([elementName isEqualToString:@"Icon"])
     return;
    if([elementName isEqualToString:@"color"])
     return;
    if([elementName isEqualToString:@"colorMode"])
     return;
    if([elementName isEqualToString:@"scale"])
     return;
    if([elementName isEqualToString:@"href"])
     return;
    if([elementName isEqualToString:@"styleUrl"])
     return;
    if([elementName isEqualToString:@"a"])
     return;

    if([elementName isEqualToString:@"Placemark"]) {
     [appDelegate.cars addObject:aCar];
     [aCar release];
     aCar = nil;
    }

    else if([elementName isEqualToString:@"name"]) {
     [aCar setValue:currentElementValue forKey:elementName];
    }

    else if([elementName isEqualToString:@"description"]) {
     [aCar setValue:currentElementValue forKey:elementName];
     [currentElementValue setString:@""];
    }
    else if([elementName isEqualToString:@"coordinates"]) {
     [aCar setValue:currentElementValue forKey:elementName];
     [currentElementValue setString:@""];
     NSLog(@"aCar coordinates:%@", [aCar coordinates]);
    }
    else
     return;
}

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

@end

/loop

A: 

Your code looks OK. Are you sure your XML contains element wrapped in Placemark tags??

Also is there more than one kml element in your xml? Could there be one that is empty which is causing your original array to be discarded and replaced with a new one of length 0?

frankodwyer
I also though this was suspicious. You are blowing away the appDelegate.cars every time you get a kml element. I don't know if you want to do that or not.
Kailoa Kadano
A: 

Don't you need to use the initWithCapacity method, in order to initialize the mutableArray properly ?

Instead of using directly appDelegate.cars, try with a local MutableArray, and see if something is different ?

Unfalkster
initWithCapacity will help you save memory if you know ahead of time how many items will be stored in the array. Personally I rarely use it unless I am creating fixed arrays that won't grow.
Kailoa Kadano