Hello everybody,
I have tried some days now to write an app where I use the NSXMLParser. I have an URL and I am able to read from the source (it is an kml-file) and print the result using NSLog. I have tried to filter the items to be printed on the screen and that works fine as well. My problem is, that no matter how I try, the NSMutableArray I use to store the data is remaining empty. To make it a bit easier to understand my problem- here is some code:
The class creating an object car:
@implementation Car
@synthesize name;
@synthesize description;
@synthesize coordinates;
@synthesize point;
- (void) dealloc {
[description release];
[coordinates release];
[name release];
[point release];
[super dealloc];
}
@end
In the class XMLParser.m is the parser and the mutableArray defined:
#import "XMLParser.h"
#import "Car2GoAppDelegate.h"
#import "Car.h"
@implementation XMLParser
@synthesize theTestArrray;
@synthesize appDelegate;
@synthesize currentElementValue;
@synthesize aCar;
- (XMLParser *) initXMLParser {
[super init];
appDelegate = (Car2GoAppDelegate *) [[UIApplication sharedApplication]delegate];
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]retain];
}
else if([elementName isEqualToString
"Placemark"]) {
aCar = [[Car alloc]init];
}
else
return;
NSLog(@"In the didStartElement method: %@", elementName);
}
- (void)parser
NSXMLParser *)parser foundCharacters
NSString *) string {
if(!currentElementValue) {
currentElementValue = [[NSMutableString alloc]initWithString:string];
}
else {
[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 {
[aCar setValue:currentElementValue forKey:elementName];
}
[currentElementValue release];
currentElementValue = nil;
}
- (void) dealloc {
[aCar release];
[currentElementValue release];
[super dealloc];
}
@end
The delegate for the whole app. I am using a rootviewcontroller containing and handling a flipsideview.
@implementation Car2GoAppDelegate
@synthesize window;
@synthesize rootViewController;
@synthesize cars;
@synthesize thisCar;
- (void)applicationDidFinishLaunching
UIApplication *)application {
NSString *URLToKml = [NSString stringWithFormat
"%@", @"http:/d9t.de/nearest/kml"];
NSURL *url = [NSURL URLWithString:URLToKml];
NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:url];
XMLParser *parser = [[XMLParser alloc]initXMLParser];
[xmlParser setDelegate
arser];
BOOL success = [xmlParser parse];
if(success) {
NSLog(@"No errors");
NSInteger *test = [cars count];
NSLog(@"The number of elements in the array:%i", test);
}
else
NSLog(@"Error by parsing");
[window addSubview:[rootViewController view]];
[window makeKeyAndVisible];
}
- (void)dealloc {
[cars release];
[rootViewController release];
[window release];
[super dealloc];
}
I can compile the program without problems and I get the control output "No errors" indicating that the parsing is ok (see above). But I do not understand why the array cars with the data always remains empty??? Can anyone help me with this problem? I have followed the tutorial here to create my app...
Thanks in advance!
loop
I have tried some days now to write an app where I use the NSXMLParser. I have an URL and I am able to read from the source (it is an kml-file) and print the result using NSLog. I have tried to filter the items to be printed on the screen and that works fine as well. My problem is, that no matter how I try, the NSMutableArray I use to store the data is remaining empty. To make it a bit easier to understand my problem- here is some code:
The class creating an object car:
@implementation Car
@synthesize name;
@synthesize description;
@synthesize coordinates;
@synthesize point;
- (void) dealloc {
[description release];
[coordinates release];
[name release];
[point release];
[super dealloc];
}
@end
In the class XMLParser.m is the parser and the mutableArray defined:
#import "XMLParser.h"
#import "Car2GoAppDelegate.h"
#import "Car.h"
@implementation XMLParser
@synthesize theTestArrray;
@synthesize appDelegate;
@synthesize currentElementValue;
@synthesize aCar;
- (XMLParser *) initXMLParser {
[super init];
appDelegate = (Car2GoAppDelegate *) [[UIApplication sharedApplication]delegate];
return self;
}
- (void) parser
if([elementName isEqualToString
appDelegate.cars = [[[NSMutableArray alloc]init]retain];
}
else if([elementName isEqualToString
aCar = [[Car alloc]init];
}
else
return;
NSLog(@"In the didStartElement method: %@", elementName);
}
- (void)parser
if(!currentElementValue) {
currentElementValue = [[NSMutableString alloc]initWithString:string];
}
else {
[currentElementValue appendString:string];
}
}
- (void) parser
if([elementName isEqualToString
return;
if([elementName isEqualToString
return;
if([elementName isEqualToString
return;
if([elementName isEqualToString
return;
if([elementName isEqualToString
return;
if([elementName isEqualToString
return;
if([elementName isEqualToString
return;
if([elementName isEqualToString
return;
if([elementName isEqualToString
return;
if([elementName isEqualToString
return;
if([elementName isEqualToString
[appDelegate.cars addObject:aCar];
[aCar release];
aCar = nil;
}
else {
[aCar setValue:currentElementValue forKey:elementName];
}
[currentElementValue release];
currentElementValue = nil;
}
- (void) dealloc {
[aCar release];
[currentElementValue release];
[super dealloc];
}
@end
The delegate for the whole app. I am using a rootviewcontroller containing and handling a flipsideview.
@implementation Car2GoAppDelegate
@synthesize window;
@synthesize rootViewController;
@synthesize cars;
@synthesize thisCar;
- (void)applicationDidFinishLaunching
NSString *URLToKml = [NSString stringWithFormat
NSURL *url = [NSURL URLWithString:URLToKml];
NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:url];
XMLParser *parser = [[XMLParser alloc]initXMLParser];
[xmlParser setDelegate
BOOL success = [xmlParser parse];
if(success) {
NSLog(@"No errors");
NSInteger *test = [cars count];
NSLog(@"The number of elements in the array:%i", test);
}
else
NSLog(@"Error by parsing");
[window addSubview:[rootViewController view]];
[window makeKeyAndVisible];
}
- (void)dealloc {
[cars release];
[rootViewController release];
[window release];
[super dealloc];
}
I can compile the program without problems and I get the control output "No errors" indicating that the parsing is ok (see above). But I do not understand why the array cars with the data always remains empty??? Can anyone help me with this problem? I have followed the tutorial here to create my app...
Thanks in advance!
loop