【问题标题】:how to remove HTML tag inside the XML tag after parsing RSS?解析RSS后如何删除XML标签内的HTML标签?
【发布时间】:2012-05-08 04:08:36
【问题描述】:

大家好,我有一个解析 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">

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

【问题讨论】:

    标签: objective-c xml parsing


    【解决方案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;
    }
    

    【讨论】:

    • 感谢您的回答,顺便说一句,您知道如何从 HTML 标记中获取图像吗:vnexpress.net/Files/Subject/3b/bd/66/e0/…\">
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2020-07-24
      • 2013-11-19
      • 2013-11-11
      • 2013-06-27
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多