有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我需要在我的RSS提要中包含HTML代码。我使用Java罗马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>

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


共 (1) 个答案

  1. # 1 楼答案

    分析

    根据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), <…>

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

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