首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用java ROME在RSS Feed中创建带有image元素的条目?

如何使用java ROME在RSS Feed中创建带有image元素的条目?
EN

Stack Overflow用户
提问于 2016-08-08 20:36:27
回答 1查看 1.6K关注 0票数 4

我正在尝试创建使用java罗马API的RSS馈送。我的要求是每个条目都应该包含一个图像,如下所示:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Sample RSS Build Results</title>
    <link>http://time.is</link>
    <description>sample RSS build</description>
        <item>
          <title>Ist Feed</title>
          <link>http://mysampleurl1.com</link>
          <description>The build was successful!</description>
          <pubDate>Mon, 08 Aug 2016 10:28:32 GMT</pubDate>
          <image>http://myimageurl1.com</image>
          <dc:date>2016-08-08T10:28:32Z</dc:date>
        </item>
        <item>
          <title>IInd Feed</title>
          <link>http://mysampleurl2.com</link>
          <description>The build was successful!</description>
          <pubDate>Mon, 08 Aug 2016 10:28:44 GMT</pubDate>
          <dc:date>2016-08-08T10:28:44Z</dc:date>
        </item>
</channel>

我是java ROME api的新手。它提供了包::com.rometools.rome.feed.synd.SyndImageImpl来设置/获取整个提要中的图像项,而不是单个条目中的图像项。对于RSS feed中的条目,它有package ::com.rometools.rome.feed.synd.SyndEntryImpl,但它没有提供任何设置或获取图像的函数。

请帮我解决这个问题。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-08-12 20:21:57

The RSS spec没有为条目指定图像元素,但是您可以使用Image namespace扩展它。

简短的解决方案可能是这样的:

代码语言:javascript
复制
SyndEntry entry = new SyndEntryImpl();
..
Element image = new Element("image", Namespace.getNamespace("image", "http://web.resource.org/rss/1.0/modules/image/"));
image.addContent("http://localhost/feed/item1_image");
entry.getForeignMarkup().add(image);

这将产生有效的xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>title</title>
    <link>http://localhost/feed</link>
    <description>description</description>
    <item>
      <title>entry title 1</title>
      <link>http://localhost/feed/item1</link>
      <image:image xmlns:image="http://web.resource.org/rss/1.0/modules/image/">http://localhost/feed/item1_image</image:image>
      <guid isPermaLink="false">http://localhost/feed/item1</guid>
    </item>
  </channel>
</rss>

更健壮的方式是像herehere那样使用create a custom module

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38829416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档