How to parse strings containing ampersands with NS

2020-05-05 15:33发布

问题:

The input string is

<path>a &amp; b</path>

and my Objective C parser code is

- (void) parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string {
    ...
    if(parserState==EXPECT_PATH) {
        NSLog(@"got %@", string);
    }
    ...
}

This prints got a instead of got a &amp; b

回答1:

parser:foundCharacters: can be called multiple times for one element's contents. It is your responsibility to append all characters from multiple runs of the method to one string.

So unless you can confirm that parser:foundCharacters: doesn't get called again right after the first run, this is expected behavior.