【问题标题】:Handle HTML in RSS XML Parser在 RSS XML 解析器中处理 HTML
【发布时间】:2015-03-01 07:31:50
【问题描述】:

我正在使用 Cocoapods 的 MWFeedParser 来解析新闻 RSS 提要。效果很好。

但是当新闻项目的描述包含 HTML(例如,用于视频嵌入)时,我不太知道如何处理它。提要网址为:DCI News Feed

以下是来自提要的 2 条新闻。第一个是不会给我带来麻烦的基本项目。第二个包含嵌入 YouTube 视频的 HTML。它使描述属性显示原始文本。我想识别 HTML 而不显示它,但要显示 HTML 后面的实际描述(在这种情况下)。

没有 HTML:

                        <item>
                            <title>12 drum corps who celebrated Christmas in July</title>
                            <description>

It�s the time of year for festive lights, merry music, and great shopping deals in stores and online. As you get set to celebrate another holiday season, you might be surprised to find that sounds of the Christmas season have long been hea...</description>
                            <link>http://www.dci.org/news/view.cfm?news_id=5571dc79-6940-42e6-a435-b4893fccc133</link>
                            <news_date>2014-12-17T10:47:00-06:00</news_date>                            
                        </item>

HTML:

<item>
                            <title>2014 Open Class World Championship video sampler</title>
                            <description>&lt;div align=&quot;center&quot;&gt;&lt;iframe width=&quot;620&quot; height=&quot;349&quot; src=&quot;//www.youtube.com/embed/fBLPYhHatGg?list=PL-1dy9pmiSAS0Z0iTONfBIJOfoewGoCvI&quot; frameborder=&quot;0&quot; allowfullscreen&gt;&lt;/iframe&gt;&lt;/div&gt;

The first corps to take the field during the Prelims competition...</description>
                            <link>http://www.dci.org/news/view.cfm?news_id=0a92bb20-9187-4689-ae92-fbe1a5a631f6</link>
                            <news_date>2014-12-17T01:24:00-06:00</news_date>                            
                        </item>

【问题讨论】:

    标签: html objective-c xml xml-parsing rss


    【解决方案1】:

    我将从我自己的 RSS 解析代码中整合我认为您需要的东西,因为没有其他答案。请随意接受另一个答案,因为我没有时间对此进行测试。

    在我的代码中,原始的&lt;description&gt; 字符串被填充到NSString* informativeText 中,除非没有描述,在这种情况下,我将&lt;title&gt; 填充到informativeText 中,所以它不为空。接下来:

    NSAttributedString* infoString = 
        [[NSAttributedString alloc] initWithHTML:
        [informativeText dataUsingEncoding:NSUnicodeStringEncoding] documentAttributes:nil];
    

    之后,您可以使用infoString.string 将 NSAttributedString 的可读文本转换为纯 NSString。

    如果您保留使用 initWithHTML 创建的 NSAtributedString,您通常可以在 NSTextView (OSX) 中显示它的合理渲染。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-06
      相关资源
      最近更新 更多