2012-05-08 94 views
1

嗨,每一個我有一個解析的RSS問題。如何在解析RSS後刪除XML標籤內的HTML標籤?

當前,我能夠解析新聞網站上的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"> 

。請告訴我如何?提前致謝。

回答

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; 
} 
+0

感謝您的回答,順便說一句,你知道如何從HTML標籤中獲取圖像: user1035877

-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; 
}