我正在使用rome 1.0为我的java应用程序生成RSS.
在我的java中:
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType( "rss_2.0" );
feed.setTitle( "My Site" );
feed.setLink( "http://example.com" );
feed.setDescription( "Test Site." );
List<SyndEntry> entries = new ArrayList<SyndEntry>();
SyndEntry entry = null;
SyndContent description = null;
entry = new SyndEntryImpl();
entry.setTitle( "Entry1" );
entry.setLink( "http://example.com/entry1" );
entry.setPublishedDate( new Date() );
description = new SyndContentImpl();
description.setType("text/html");
description.setValue( "This is the content of entry 1." );
entry.setDescription( description );
entries.add( entry );
feed.setEntries(entries);
Writer writer = new FileWriter("/home/jr/Desktop/stream.xml");
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed,writer);
writer.close();
Run Code Online (Sandbox Code Playgroud)
生成的RSS:
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title>My Site</title>
<link>http://example.com</link>
<description>Test Site.</description>
<item>
<title>Entry1</title>
<link>http://example.com/entry1</link>
<description>This is the content of entry 1.</description>
<pubDate>Fri, 09 Nov 2012 01:28:57 GMT</pubDate>
<guid>http://example.com/entry1</guid>
<dc:date>2012-11-09T01:28:57Z</dc:date>
</item>
</channel>
</rss>
Run Code Online (Sandbox Code Playgroud)
在此处验证RSS时,它具有以下建议:
如何在罗马图书馆做推荐?生成的RSS好吗?
谢谢.
发生这种情况是因为SyndFeedImpl日期和publishedDate字段使用相同的字段(来自DC模块):
@Override
public Date getPublishedDate() {
return getDCModule().getDate();
}
@Override
public void setPublishedDate(final Date publishedDate) {
getDCModule().setDate(publishedDate);
}
Run Code Online (Sandbox Code Playgroud)
由于RSS093Generator(由 所使用RSS20Generator)从指定项创建 pubDate 元素,而且还继承自 DCModuleGenerator,因此您会得到以下两个结果:
final Date pubDate = item.getPubDate();
if (pubDate != null) {
eItem.addContent(generateSimpleElement("pubDate", DateParser.formatRFC822(pubDate, Locale.US)));
}
Run Code Online (Sandbox Code Playgroud)
和这个:
final Date dcDate = dcModule.getDate();
if (dcDate != null) {
for (final Date date : dcModule.getDates()) {
element.addContent(generateSimpleElement("date", DateParser.formatW3CDateTime(date, Locale.US)));
}
}
Run Code Online (Sandbox Code Playgroud)
SyndFeed可以通过实施您自己的自定义来防止这种交互。在这种情况下,您需要做的就是创建一个类变量来存储您的 pubDate,以便DCModule永远不会获得日期集,因此永远不会生成不需要的dc:date元素。
我遇到了完全相同的问题并通过使用以下SyndFeed实现解决了它:
public class CustomSyndFeed extends SyndFeedImpl {
protected Date publishedDate;
@Override
public Date getPublishedDate() {
return publishedDate;
}
@Override
public void setPublishedDate(final Date publishedDate) {
this.publishedDate = new Date(publishedDate.getTime());
}
}
Run Code Online (Sandbox Code Playgroud)