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

Java ROME RSS library and HTML code in RSS description field

我需要将 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.

description 对比 content: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.