我遵循了一个教程,并且 XML 解析部分工作正常。我的问题是,当打印出我存储到对象中的值(在我的情况下,对象是汽车)时,始终保持为空。我将所有对象保存到一个 NSMutableArray 中,它似乎总是有一个长度/计数 == 0 ...我做错了什么?从代码中可以看出,该数组在委托类中定义......
- (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
/环形