Hey! i'm using the NSXMLParser to fetch a String from xml. I'v created a class to store the data with synchronized variables. To get the text between the elementName i use the foundCharacter function. And to store the Strings i use a MutableString *. When i find the String and print everything is correct but when i'm done the two different Strings become the same (the last parsed String) in some mysterious way. I have even tried to save the Strings to an array to be sure they are not replaced somehow but even though the Strings become the same??? How can this happen?
PLEASE EXPLAIN SOMEONE!
[xml]
<'name'>USA<'/name'>
<'city'>NEW YORK<'/city'>
[/xml]
[objec*]
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
//PARSE CITY AND COUNRYNAME if([elementName isEqualToString:@"name"] || [elementName isEqualToString:@"countryName"]){ PARSE_CHARACTER = TRUE; [currentParsedCharacterData setString:@""]; }
}
(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"name"]){
dataObject.name = currentParseBatch;
NSLog(@"NAME: %@", dataObject.name); //OUTPUT: NAME: USA
}
if([elementName isEqualToString:@"city"]){
dataObject.city = currentParseBatch;
NSLog(@"CITY: %@", dataObject.city); //OUTPUT: CITY: NEW YORK
}
PARSE_CHARACTER = FALSE;
}
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"name"] || [elementName isEqualToString:@"city"]){
PARSE_CHARACTER = TRUE;
[currentParseBatch setString:@""];
}
if(DONE){
[parser abortParsing];
NSLog(@"NAME: %@", dataObject.name); //OUTPUT: NAME: NEW YORK
NSLog(@"CITY: %@", dataObject.city); //OUTPUT: CITY: NEW YORK
}
}
(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (PARSE_CHARACTER) {
[currentParseBatch appendString:string];
}
}
[/objec*]