Trimming characters of NSXMLParser

793 views Asked by At

What is the right way to trim characters in foundCharacters of NSXMLParser?

I've found many examples that do it like this:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
        NSString *characters = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        // Append to previous characters of the same element
}

Yet a space followed by a non-ASCII character is trimmed when I run the above. Example: "this é" will become "thisé".

2

There are 2 answers

0
Jack Cox On

This is what I've used in the past to trim whitespace without any problems.

- (NSString *)trim:(NSString *)inStr {
    return [inStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

}

The only difference is that It's using a slightly different whitespace character set.

1
Matthias Bauch On

Yet a space followed by a non-ASCII character is trimmed when I run the above. Example: "this é" will become "thisé".

maybe by coincidence.

See the discussion of parser:foundCharacters:

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.

And if the you trim the whitespace from this (with a space at the end you can't see) and then from é you will end up with thisé.

I've never used NSXMLParser, but maybe you should trim the string in parser:didEndElement:namespaceURI:qualifiedName:. From what I understand, the element will be completed then.

But I'm just guessing.