Java ROME RSS 库和 RSS 描述字段中的 HTML 代码

标签 java rss rome

我需要将 HTML 代码包含到我的 RSS 源中。我使用 Java ROME RSS 库:

SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");

feed.setTitle("Title");
feed.setLink("example.com");
feed.setDescription("Description");

List<SyndEntry> entries = new ArrayList<>();

SyndEntryImpl entry = new SyndEntryImpl();
entry.setTitle("Name");

SyndContent syndContent = new SyndContentImpl();
syndContent.setType("text/html");
syndContent.setValue("<p>Hello, World !</p>");

entry.setDescription(syndContent);

entries.add(entry);

feed.setEntries(entries);

Writer writer = new FileWriter("rss.xml");
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed, writer);
writer.close();

但输出 XML 包含编码描述:

<description>&lt;p&gt;Hello, World !&lt;/p&gt;</description>

如何在 ROME 中正确包含未编码的 HTML 代码?

最佳答案

分析

根据RSS Best Practices Profile: 4.1.1.20.4 description :

The description must be suitable for presentation as HTML. HTML markup must be encoded as character data either by employing the HTML entities &lt; ("<") and &gt; (">") or a CDATA section.

因此,当前输出是正确的。

CDATA编码

如果需要CDATA部分( CDATA 编码),可以使用以下代码:

final List<String> contents = new ArrayList<>();
contents.add("<p>HTML content is here!</p>");

final ContentModule module = new ContentModuleImpl();
module.setEncodeds(contents);

entry.getModules().add(module);

其他引用

  1. RSS Best Practices Profile .
  2. Putting content:encoded in RSS feed using ROME - Stack Overflow .
  3. Re: CDATA Support - Mark Woodman - net.java.dev.rome.dev - MarkMail .
  4. rome-modules/ContentModuleImplTest.java at master · rometools/rome-modules · GitHub .

descriptioncontent:encoded

Should I use both - description and content:encoded nodes or only one of them in my RSS feed item ?

And how about the following?

An item may also be complete in itself, if so, the description contains the text (entity-encoded HTML is allowed; see examples), <…>

根据RSS 2.0规范,使用description元素就足够了:正如您所引用的那样。以下是示例:Encoding & item-level descriptions (RSS 2.0 at Harvard Law) .

有关更多详细信息,请参阅问题:Difference between description and content:encoded tags in RSS2 - Stack Overflow .

关于Java ROME RSS 库和 RSS 描述字段中的 HTML 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48469810/

相关文章:

ruby-on-rails - 使用 FeedJira 创建 RSS 聚合器/阅读器

java - 尽管设置了用户代理,但来自 Java 应用程序(但不是 Web 浏览器)的 HTTP 403

java - RSS Feed - 解析结束标记时发生异常

java - 如何重用java代码块

spring - 无法从 Java Spring 中的 RSS Feed 接收数据

java - JUnit5 - 有没有可靠的方法来获取已执行测试的类

javascript - 使用 Google Feeds API 提取 RSS 提要标题

java - 使用 ROME 搜索 RSS 源

java - Oracle PL/SQL 中过程/函数和对象之间的区别

java - junit 测试 Iterable 的相等性