Hello all and happy new year to start with!
I have a question regadring the parsing of an XML file.
I have an application whicht uses the parsing code below. Everything works all right if I run the code below. The problem is that I have an update function in my app, meaning the user may re-parse the xml file as many times he wants during the program-run. If I release the current-element-variable as in the code below, the app crashes. I use the same parser object as I did the first time and I load the data with NSURLConnection- i.e when the connection is ready is the parser next to do his work.
Why can I not release the object as I do in my code?
It must be a huge memory leak if I do not release this object or did I miss something here?
And, is it all right to release the currentNodeContent variable when the parser is done?
I hope someone has some good advice? or ideas?
Thanks in advance,
MACloop
I have a question regadring the parsing of an XML file.
I have an application whicht uses the parsing code below. Everything works all right if I run the code below. The problem is that I have an update function in my app, meaning the user may re-parse the xml file as many times he wants during the program-run. If I release the current-element-variable as in the code below, the app crashes. I use the same parser object as I did the first time and I load the data with NSURLConnection- i.e when the connection is ready is the parser next to do his work.
Why can I not release the object as I do in my code?
It must be a huge memory leak if I do not release this object or did I miss something here?
And, is it all right to release the currentNodeContent variable when the parser is done?
I hope someone has some good advice? or ideas?
Thanks in advance,
MACloop
Code:
#pragma mark -
#pragma mark Parser Delegate Methods
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentNodeContent = [[NSMutableString alloc] init];//
if([elementName isEqualToString:@"StartTag"]) {//a new item
currentElement = [[CurrentElement alloc]init];//allocting
}
if([elementName isEqualToString:@"Data"]){
nameString = [attributeDict objectForKey:@"name"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:@"number"]) {
[currentElement setValue:currentNodeContent forKey:@"number"];
}
if([elementName isEqualToString:@"description"]) {
[currentElement setValue:currentNodeContent forKey:@"description"];
}
if([elementName isEqualToString:@"Data"]) {
if([nameString isEqualToString:@"street"]) {
[currentElement setValue:currentNodeContent forKey:@"street"];
}
if([nameString isEqualToString:@"town"]){
[currentElement setValue:currentNodeContent forKey:@"town"];
}
if([nameString isEqualToString:@"mail"]){
[currentElement setValue:currentNodeContent forKey:@"mail"];
}
}
if([elementName isEqualToString:@"StartTag"]) {
[arrayWithAllObjects addObject:currentElement];
[currentElement release];
}
else
return;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[currentNodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[currentNodeContent release];
//doing the rest here...
}
- (void)parser:(NSXMLParser *)p parseErrorOccurred:(NSError *)parseError {
[p abortParsing];
parserProblems = TRUE;
UIAlertView *alert = [[[UIAlertView alloc]initWithTitle:@"Error" message:@"Problems" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]autorelease];
[alert show];
}