1

大家好,我有一个解析 RSS 的问题。

目前,我能够解析新闻网站上的 RSS XML,并将其显示在 UITableViewCell 中。我解析描述标签是:

<description><![CDATA[ <a href="http://vnexpress.net/gl/xa-hoi/2012/05/thuy-dien-song-tranh-de-bi-lo-sut-dat-1/"><img src="http://vnexpress.net/Files/Subject/3b/bd/66/e0/sat-lo-dat-Song-Tranh-2.jpg"></a>this is new<BR />></description>

现在的问题是我怎样才能得到这个标签内的文本?目前它显示描述标签内的所有内容,即:

<![CDATA[ <a href="http://vnexpress.net/gl/xa-hoi/2012/05/thuy-dien-song-tranh-de-bi-lo-sut-dat-1/"><img src="http://vnexpress.net/Files/Subject/3b/bd/66/e0/sat-lo-dat-Song-Tranh-2.jpg"></a>this is new<BR />>

我只想显示纯文本:

this is new

而且我还想在此描述标签中获取图像,以便显示它:

<img src="http://vnexpress.net/Files/Subject/3b/bd/66/e0/sat-lo-dat-Song-Tranh-2.jpg">

. 请告诉我怎么做?提前致谢。

4

2 回答 2

1

我以前必须这样做。所以我将粘贴我在这里使用的代码。

- (NSString *)removeHTMLTags:(NSString *)str
{   
NSMutableString *temp_str = [[NSMutableString alloc] initWithString:str];
NSRange openTag = [temp_str rangeOfString:@"<"];
NSRange closeTag = [temp_str rangeOfString:@">"];

while (openTag.length > 0) {
    NSRange range;
    range.location = openTag.location;
    range.length = (closeTag.location - openTag.location) + 1;
    [temp_str setString:[temp_str stringByReplacingCharactersInRange:range withString:@""]];

    openTag = [temp_str rangeOfString:@"<"];
    closeTag = [temp_str rangeOfString:@">"];
}

[temp_str replaceOccurrencesOfString:@"&Auml;" withString:@"Ä" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
[temp_str replaceOccurrencesOfString:@"&Aring;" withString:@"Å" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
[temp_str replaceOccurrencesOfString:@"&AElig;" withString:@"Æ" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];


while ([temp_str rangeOfString:@"  "].location != NSNotFound) {
    [temp_str replaceOccurrencesOfString:@"  " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}

while ([temp_str rangeOfString:@" ."].location != NSNotFound) {
    [temp_str replaceOccurrencesOfString:@" ." withString:@"." options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}

while ([temp_str rangeOfString:@" ,"].location != NSNotFound) {
    [temp_str replaceOccurrencesOfString:@" ," withString:@"," options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}

while ([temp_str rangeOfString:@" ;"].location != NSNotFound) {
    [temp_str replaceOccurrencesOfString:@" ;" withString:@";" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}


return temp_str;
}
于 2012-05-08T04:25:22.383 回答
-1

对于 iOS 7+,您可以使用NSAttributedString如下:

[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];

对于低于 iOS 7 的版本,请使用此代码删除 < 和 > 之间的所有内容

(NSString *) stringByStrippingHTML {
  NSRange r;
  NSString *s = [[self copy] autorelease];
  while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
    s = [s stringByReplacingCharactersInRange:r withString:@""];
  return s;
}
于 2012-05-08T04:16:18.113 回答