2012-11-09 41 views
6

Im使用rome 1.0爲我的Java應用程序生成RSS。有效的RSS 2.0使用羅馬

在我的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(); 

生成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> 

當RSS被驗證here,它具有以下建議:

  • 項目應不包括pubDate和dc:日期
  • M issing原子:與rel =「self」鏈接

如何在羅馬圖書館推薦?生成的RSS是否正常?

謝謝。

+0

部分回答[原子:聯繫在使用羅馬RSS](http://stackoverflow.com/questions/18112949/atomlink-in-rss-using-rome)。 – Joe

+0

只是想提一下,@ JoshC13的答案確實有效,但它應該應用於'SyndEntryImpl'而不是'SyndFeedImpl',因爲重複的日期發生在''元素 –

回答

1

所以發生這種情況,因爲SyndFeedImpl使用相同的字段的日期和publishedDate字段(從DC模塊):

@Override 
public Date getPublishedDate() { 
    return getDCModule().getDate(); 
} 

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

由於RSS093Generator(由RSS20Generator使用)創建從一個pubdate的元件指定的項目,但也從DCModuleGenerator繼承,你都這樣:

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

這:

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

通過實現您自己的自定義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()); 
    } 
} 
2

在您的自定義SyndFeed類,請確保你的名字你日期變量不同於在SyndFeed類什麼的(即:而不是「publishedDate」,使用類似「pubdate的」這似乎已經解決了這個問題對我來說

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

Hello Happy編碼器!我看到你是新來的,只是單挑而已。我們通常會嘗試爲新技術或方法保留答案,併爲小增補使用註釋,例如更改變量名稱,因爲它們不代表替代方法。我瞭解你的困境,因爲信譽要求現在把評論部分放在禁區內。也許在這裏陳述你的問題:http://meta.stackexchange.com/questions/12119/lower-the-amount-of-reputation-needed-to-comment會很有用。 – JoshC13

1

小遲到了,但在這裏沒有答案開箱,所以我想通。我會加我的。

@Vitor在他的評論中指出了這樣做的正確方法,以延長SyndEntryImpl並將其用作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()); 
    } 
}