我有一个iPhone应用程序,这就是我使用NSXMLParser解析RSS提要中的标题:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentString = [[NSMutableString alloc] init];
[self setTitle:currentString];
}
这完全没问题。现在我正在处理另一个包含标签的Feed,其中包含了标签,我想删除它们。我在StackOverflow上看到过像THIS这样的不同问题,它们给出了用空字符串替换
标签的代码。这就是我修改代码的方式:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentString = [[NSMutableString alloc] init];
currentString = [[currentString stringByReplacingOccurrencesOfString:@"<br>" withString:@""] mutableCopy];
[self setTitle:currentString];
}
但这不起作用,并且在输出中显示相同的文本而不删除
标签。任何人都可以指出我做错了什么,我怎么能纠正它?
答案 0 :(得分:0)
我认为您应该尝试其中一种可能对您有帮助的功能
#pragma mark -
#pragma mark - Remove HTML Tags
- (NSString *)stringByStrippingHTML:(NSString *)inputString
{
NSMutableString *outString = [[[NSMutableString alloc] initWithString:inputString] autorelease];
if ([inputString length] > 0)
{
NSRange r;
while ((r = [outString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
{
[outString deleteCharactersInRange:r];
}
}
return outString;
}
- (NSString *)stripTags:(NSString *)str
{
NSMutableString *html = [NSMutableString stringWithCapacity:[str length]];
NSScanner *scanner = [NSScanner scannerWithString:str];
scanner.charactersToBeSkipped = NULL;
NSString *tempText = nil;
while (![scanner isAtEnd])
{
[scanner scanUpToString:@"<" intoString:&tempText];
if (tempText != nil)
[html appendString:tempText];
[scanner scanUpToString:@">" intoString:NULL];
if (![scanner isAtEnd])
[scanner setScanLocation:[scanner scanLocation] + 1];
tempText = nil;
}
return html;
}