Objective c 解析RSS后如何删除XML标记中的HTML标记?
大家好,我有个问题 目前,我能够解析新闻web上的RSS XML,并将其显示在UITableViewCell中。我解析描述标记,它是:Objective c 解析RSS后如何删除XML标记中的HTML标记?,objective-c,xml,parsing,Objective C,Xml,Parsing,大家好,我有个问题 目前,我能够解析新闻web上的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/
<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 />>
我还想得到这个描述标签中的图像,这样我就可以显示它了:
<img src="http://vnexpress.net/Files/Subject/3b/bd/66/e0/sat-lo-dat-Song-Tranh-2.jpg">
。请告诉我怎么做?提前感谢。对于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;
}
我以前必须这样做。因此,我将粘贴我在这里使用的代码
- (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:@"Ä" withString:@"Ä" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
[temp_str replaceOccurrencesOfString:@"Å" withString:@"Å" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
[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])];
}
while ([temp_str rangeOfString:@" ;"].location != NSNotFound) {
[temp_str replaceOccurrencesOfString:@" ;" withString:@";" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}
return temp_str;
}
谢谢您的回答,顺便问一下,您知道如何从HTML标签中获取图像,如下所示:
- (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:@"Ä" withString:@"Ä" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
[temp_str replaceOccurrencesOfString:@"Å" withString:@"Å" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
[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])];
}
while ([temp_str rangeOfString:@" ;"].location != NSNotFound) {
[temp_str replaceOccurrencesOfString:@" ;" withString:@";" options:NSLiteralSearch range:NSMakeRange(0, [temp_str length])];
}
return temp_str;
}