在RSS XML Parser中处理HTML

时间:2015-01-03 02:19:46

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

我正在使用Cocoapods的MWFeedParser来解析新闻RSS提要。工作得很漂亮。

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

以下是Feed中的2条新闻。第一个是基本项目,不会给我带来麻烦。第二个包含用于嵌入YouTube视频的HTML。它使description属性显示原始文本。我想识别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>

1 个答案:

答案 0 :(得分:0)

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

在我的代码中,原始<description>字符串被填入NSString* informativeText,除非没有说明,在这种情况下,我将<title>填入informativeText所以它不是空。接下来:

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

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

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

相关问题