views:

29

answers:

1

I'm currently parsing an xml file that has a string like this between two elements:

Hello & bye

When the foundCharacters delegate gets called it parses the information like this....

it parses:

Hello & bye

It makes 3 calls to parse that element and I end up with 3 strings as opposed to 1 string. Is there some way to detect this with out having to append a string together and keep a counter of how many times the delegate was called?

Any help is very appreciated...

+2  A: 

Hi,

Short answer : No. It's only chance that it's returning three strings, it might return 11 strings all one character long if it wanted to. It's up to you to join the strings together.

Long answer : You need to append a string. There's nothing you can do about it. however, I don't understand why you need to keep a count of the number of times the delegate has been called - I think that this code should do what you want :

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    // Make a new string to hold the data
    [currentString release];
    currentString = [NSMutableString alloc] init];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    // Add the sitrng to our current string
    [currentString appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    // In here, current string should be it's entire contents:
    NSLog(@"%@", currentString);
}

You will need a member in your class to hold the data - a mutable string is easiest :

@interface MyClass : NSObject {
    NSMutableString *currentString;
}
@end

(It doesn't need to be a property but you do need to remember to release it in your dealloc method)

deanWombourne
Toret