我需要将 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><p>Hello, World !</p></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
<
("<"
) and>
(">"
) or aCDATA
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);
其他引用
- RSS Best Practices Profile .
- Putting content:encoded in RSS feed using ROME - Stack Overflow .
- Re: CDATA Support - Mark Woodman - net.java.dev.rome.dev - MarkMail .
- rome-modules/ContentModuleImplTest.java at master · rometools/rome-modules · GitHub .
description
与 content:encoded
Should I use both -
description
andcontent:encoded
nodes or only one of them in my RSS feed item ?And how about the following?
block 引用>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/