[opencms-dev] HowTo Display (external) RSS Feed in OpenCms Template

Mathias Lin | SYSVISION mail at mathiaslin.com
Thu Sep 10 06:55:21 CEST 2009


Since this is a very common feature for many websites, here's a useful
snippet I usually use when displaying RSS feeds in an OpenCms template and
would like to share. For example you want to display an RSS feed from an
external WordPress blog into your OpenCms website.

The OpenCms schema has a OpenCmsString input field (named BlogRssUrl in my
case), which is mapped to property BlogRssUrl as well.


<xsd:complexType name="OpenCmsMySchema">
  ...
  <xsd:element name="BlogRssUrl" type="OpenCmsString" minOccurs="0"
maxOccurs="1" />
  ...
</xsd:complexType>

<mappings>
    <mapping element="BlogRssUrl" mapto="property:BlogRssUrl" />
</mappings>


For fetching the RSS feed, I recommend the ROME Fetcher (supports all common
rss standards) subproject at library https://rome.dev.java.net/, the project
only depends on the JDOM parser (jdom.jar). So you will end up deploying
three additional jars into the OpenCms app:

    * jdom.jar
    * rome-1.0RC2.jar (or newer version)
    * rome-fetcher-0.9.jar (or newer version)



Below is the scriplet then for the OpenCms jsp template:


<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
<%@ page session="false"
import="org.opencms.jsp.*,java.util.*,org.opencms.file.*,java.net.URL,com.sun.syndication.feed.synd.*,com.sun.syndication.fetcher.*,com.sun.syndication.fetcher.impl.*"
%>
<%  
    CmsJspActionElement cms = new CmsJspActionElement(pageContext, request,
response);
    String filename = cms.getRequestContext().getUri();

    // === RSS Feed Fetching ===
    FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
    FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache);
    CmsObject cmso = cms.getCmsObject();
    String blogRssUrl = cmso.readProperty(filename, "BlogRssUrl");
    SyndFeed feed = null;
    if (blogRssUrl!=null && !"".equals(blogRssUrl)) feed =
feedFetcher.retrieveFeed(new URL(blogRssUrl));
%>


and then display the output on the page:


<% if (feed!=null) { %>
<ul>
<%
// === RSS feed output ===
for (Iterator<SyndEntry> iter= feed.getEntries().iterator();
iter.hasNext();) {
    SyndEntry entry = iter.next();
    %>               
    <li> "<%=entry.getUri()% "><%=entry.getTitle()%> </li>           
    <%               
}
%>
</ul>
<% } %>


-----
Mathias Lin
SYSVISION Ltd., China
http://www.sysvision.com
-- 
View this message in context: http://www.nabble.com/HowTo-Display-%28external%29-RSS-Feed-in-OpenCms-Template-tp25377275p25377275.html
Sent from the OpenCMS - Dev mailing list archive at Nabble.com.




More information about the opencms-dev mailing list