Valid RSS 2.0 Using Rome

Im using rome 1.0 to generate RSS for my java application.

In my 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(); 

Generated 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> 

When RSS is confirmed here , it has the following recommendations:

  • The element must not contain both pubDate and dc: date
  • Atom missing: link with rel = "self"

How to make a recommendation in the rome library? Is the generated RSS ok?

Thanks.

+6
source share
3 answers

In your custom SyndFeed class, make sure you specify the Date variable differently than in the SyndFeed class (Ie: instead of publishDate), use something like pubDate. This seems to have solved the problem for me.

 public class CustomSyndFeed extends SyndFeedImpl { protected Date pubDate; @Override public Date getPublishedDate() { return pubDate; } @Override public void setPublishedDate(final Date pubDate) { this.pubDate = new Date(pubDate.getTime()); } } 
+2
source

So, this is because SyndFeedImpl uses the same field for the date and publishedDate fields (from the DC module):

 @Override public Date getPublishedDate() { return getDCModule().getDate(); } @Override public void setPublishedDate(final Date publishedDate) { getDCModule().setDate(publishedDate); } 

Since RSS093Generator (used by RSS20Generator ) creates the pubDate element from the specified element, but also inherits from DCModuleGenerator, you get both of these:

 final Date pubDate = item.getPubDate(); if (pubDate != null) { eItem.addContent(generateSimpleElement("pubDate", DateParser.formatRFC822(pubDate, Locale.US))); } 

and this:

 final Date dcDate = dcModule.getDate(); if (dcDate != null) { for (final Date date : dcModule.getDates()) { element.addContent(generateSimpleElement("date", DateParser.formatW3CDateTime(date, Locale.US))); } } 

This interaction can be prevented by implementing your own SyndFeed your own. In this case, all you have to do is create a class variable to hold your pubDate so that DCModule never sets the date and therefore never generates your unwanted dc:date element.

I ran into the same problem and solved it using this SyndFeed implementation:

 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()); } } 
+1
source

A little late for the party, but the answer here does not work, so I decided that I would add my own.

As @Vitor points out in his comment, the right way to do this is to extend SyndEntryImpl and use it as SyndEntry entry = new CustomEntryImpl(); .

 public class CustomEntryImpl extends SyndEntryImpl { protected Date pubDate; @Override public Date getPublishedDate() { return pubDate; } @Override public void setPublishedDate(final Date pubDate) { this.pubDate = new Date(pubDate.getTime()); } } 
+1
source

All Articles