RSS中的CDATA部分无法正常工作 - rometools

时间:2016-03-15 07:36:34

标签: java spring-mvc rss rome

我正在使用rometools作为rss feed。我试图在RSS项目的内容部分中使用cdata内的html标签。这是我的代码:

public class RssView extends AbstractRssFeedView {
@Override
 protected List<com.rometools.rome.feed.rss.Item> buildFeedItems(Map<String, Object> map,
                                                                HttpServletRequest httpServletRequest,
                                                                HttpServletResponse httpServletResponse) throws Exception {
    List<Item> items = new ArrayList<>();
    Object ob = map.get("feeds");
    if (ob instanceof List){
        for(int i = 0; i < ((List<?>)ob).size(); i++){
            Object articleObj = ((List<?>) ob).get(i);

            Article article = (Article)articleObj;
            Item item = new Item();
            item.setTitle(article.getTitle());

            Guid guid = new Guid();
            guid.setValue(item.getLink());
            item.setGuid(guid);

            item.setPubDate(article.getCreatedTime());

            Description description = new Description();
            description.setValue(article.getDescrition());
            item.setDescription(description);

            Content content = new Content();
            content.setValue(buildContent(article));
            item.setContent(content);
            items.add(item);
        }
    }
    return items;
}
@Override
protected void buildFeedMetadata(Map<String, Object> model, Channel channel,
                                 HttpServletRequest request) {
    channel.setTitle("Article");
    channel.setLink("http://www.");
    channel.setDescription("desciprtion");
    channel.setLanguage("en-us");
}

private String buildContent(Article article) {
    StringBuilder sb = new StringBuilder();

    sb.append("<![CDATA[" +
            "<!doctype html>\n]]>");

    return sb.toString();
}

问题是cdata中不应该被转义的html标签正在被转义。

2 个答案:

答案 0 :(得分:1)

你想要做的事情,目前罗马是不可能做到的。请参阅此问题:https://github.com/rometools/rome/issues/280

答案 1 :(得分:0)

我最终使用

channel.setDescription(StringEscapeUtils.escapeXml11(getProblematicDescription()));

来自

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8.1</version>
</dependency>