您不能停止被调用,但是如果是您关心的两个元素之一,foundCharacters
您可以didStartElement
设置一些类属性,然后查看该类属性以确定它是否应该对这些字符执行某些操作,或者是否它应该立即返回并有效地丢弃它收到的字符。elementName
foundCharacters
例如,这是我的解析器的简化版本:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
// if the element name is in my NSArray of element names I care about ...
if ([self.elementNames containsObject:elementName])
{
// then initialize the variable that I'll use to collect the characters.
self.elementValue = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
// if the variable to collect the characters is not nil, then append the string
if (self.elementValue)
{
[self.elementValue appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
// if the element name is in my NSArray of element names I care about ...
if ([self.elementNames containsObject:elementName])
{
// step 1, save the data in `elementValue` here (do whatever you want here)
// step 2, reset my elementValue variable
self.elementValue = nil;
}
}
希望这能给你这个想法。