1

I'm developing an application that needs to get data from an XML file. Some of the nodes have a lot of characters and I've a problem using this function :

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

For example for the description node of an item I will get only the 20-30 last characters whereas I would get 200 or 300.

I checked this out with a NSLog and it appears the problem comes from here. Do you know what's wrong ?

Thanks for any advice.

Rob
  • 15,732
  • 22
  • 69
  • 107

1 Answers1

2

SAX parsers do not guarantee to get all characters at once. You may get multiple calls with chunks of characters from any given block; your code should concatenate them into a single string.

The parser object may send the delegate several parser:foundCharacters: messages to report the characters of an element. Because string may be only part of the total character content for the current element, you should append it to the current accumulation of characters until the element changes.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    if ([qualifiedName isEqualToString:@"myTag"]) {
        buf = [NSMutableString string];
    }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if ([qualifiedName isEqualToString:@"myTag"]) {
        buf = [buf stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSLog(@"Got %@", buf);
    }
}

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [buf appendString:string];
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thanks for your answer. Could you give me a little code example of what should replace stringByTrimmingCharactersInSet: to get characters one by one or blocks by blocks ? – Rob May 27 '12 at 22:05
  • 1
    You should move your trimming code to the `parser:didEndElement:namespaceURI:qualifiedName:` method of the delegate. In the "start element" method you should clear the buffer, and in the `foundCharacters` method you should simply append `string` to a mutable string buffer by calling `appendString:`. – Sergey Kalinichenko May 27 '12 at 22:08
  • And that worked perfectly but I also had to declare buf as a property instead of an ivar. Thank you very much again. – Rob May 27 '12 at 22:22