Jump to content
Changes to the Jaspersoft community edition download ×
  • How to show an RSS feed


    Giulio Toffoli
    • Version: v3.6.0 Product: iReport Designer

    by Giulio Toffoli

    This tutorial introduces the XPath2 query language and explains how to use a remote XML as data source for a report. This approach can be easily used to display an RSS feed using a report.

    An RSS file is an XML file used to publish frequently updated works like blog entries and news headlines.

    Here is an extract from an RSS provided by BBC for the world news

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml-stylesheet title="XSL_formatting" type="text/xsl" href="/shared/bsp/xsl/rss/nolsol.xsl"?>
    <rss xmlns:media="http://search.yahoo.com/mrss" version="2.0">
      <channel>
        <title>BBC News | News Front Page | World Edition</title>
        <link>http://news.bbc.co.uk/go/rss/-/2/hi/default.stm</link>
        <description>Get the latest BBC World news: international news, features and analysis from Africa, Americas, South Asia, Asia-Pacific, Europe and
                              the Middle East.</description>
        <language>en-gb</language>
        <lastBuildDate>Tue, 08 Sep 2009 10:29:59 GMT</lastBuildDate>
        <copyright>Copyright: (C) British Broadcasting Corporation, see http://news.bbc.co.uk/2/hi/help/rss/4498287.stm for terms and conditions of
                           reuse</copyright>
        <docs>http://www.bbc.co.uk/syndication/</docs>
        <ttl>15</ttl>
        <image>
          <title>BBC News</title>
          <url>http://news.bbc.co.uk/nol/shared/img/bbc_news_120x60.gif</url>
          <link>http://news.bbc.co.uk/go/rss/-/2/hi/default.stm</link>
        </image>
        <item>
          <title>UN warns Afghans over poll fraud</title>
          <description>The UN calls for a crackdown on Afghan electoral fraud as poll officials order a number of recounts of questionable results.
          </description>
          <link>http://news.bbc.co.uk/go/rss/-/2/hi/south_asia/8243276.stm</link>
          <guid isPermaLink="false">http://news.bbc.co.uk/2/hi/south_asia/8243276.stm</guid>
          <pubDate>Tue, 08 Sep 2009 10:11:21 GMT</pubDate>
          <category>South Asia</category>
          <media:thumbnail width="66" height="49" 
                                       url="http://newsimg.bbc.co.uk/media/images/46338000/jpg/_46338717_007872880-1.jpg"/>
        </item>
        <item>
          <title>'Ill' pilots ground India airline</title>
          <description>India's second-largest private airline Jet Airways cancels many flights after pilots stage a flash protest and report sick.</description>
          <link>http://news.bbc.co.uk/go/rss/-/2/hi/south_asia/8243264.stm</link>
          <guid isPermaLink="false">http://news.bbc.co.uk/2/hi/south_asia/8243264.stm</guid>
          <pubDate>Tue, 08 Sep 2009 08:38:23 GMT</pubDate>
          <category>South Asia</category>
          <media:thumbnail width="66" height="49" 
                                       url="http://newsimg.bbc.co.uk/media/images/45109000/jpg/_45109260_jet_afp66.jpg"/>
        </item>
      </channel>
    </rss>
    

    Let's start creating two data sources: the first will be used only at design time, I call it "BBC World News (Local)", and it is a simple XML data source. Be sure you have a local version of the RSS feed xml (you can download it from the BBC site, I had to to replace all the occurrences of & in & in order to make iReport able to correctly parse the file).
    The second data source ("BBC World News") must be a remote XML data source which will point to the real rss url.
    Both have set the Date pattern to EEE, dd MMM yyyy HH:mm:ss z (which can be used to translate a date like Tue, 08 Sep 2009 09:35:12 GMT).

    figure_1.png.d3bf08860f8ed8d1e91bd49f8f3f73f4.pngfigure_2.png.f46b7bc813972567e719718f7852efd8.png
    Figure 1 and 2

    Open the query dialog and add the some fields like in the following picture. The record node must be set to the "item" tag.

    figure_3.png.241feb55d6645ddb979adbfd0ce99136.png
    Figure 3

    Select for instance the RSS title and description (which are not part of the item node), the item title, description and the pubDate. Close the dialog. Select the fields and set a better names using the property sheet. For the pubData field (which I renamed in Item_date) set as class type: java.util.Date.

    figure_4.png.8e0628211cbfa9229776432ed4962055.png
    Figure 4

     

    Design the layout adding the RSS_title and RSS_description in the title band, and the Item_Title, Item_Description and Item_date in the detail band.
    For each textfield set the property Stretch With Overflow to true (since we have not idea how many lines of text will have each news), and set your favorite date pattern for the textfield showing the date.

     

    figure_5.png.6f73de94c6a4f784148445a6d1c4a3c3.png
    Figure 5

     

    Run your report, the result should be similar to the one shown in the following picture.

     

    figure_6.png.1596dc700262feb53563c8fcb89b643f.png
    Figure 6

    Now change the query language by opening the query dialog and setting as language xpath2. Save the report and change the active connection to the remote one and run the report again. This time the remote xml will be used to fill the report.

    The XPath2 query language is the same as XPath. What it changes is the query executer behind the language, in other words the engine that process the query. The XPath2 query executer is able to download the XML file from a remote url. We set the url when we defined the data source, but the url, as optional GET and POST parameters can be specified as parameters for the report. When the report is executed inside an application, the parameter XML_URL can be set dynamically to the URL to use. Other parameters that can be set include: XML_USERNAME and XML_PASSWORD for http authentication and XML_DATE_PATTERN and XML_NUMBER_PATTERN to set the patter to convert dates and numbers from text.

    POST and GET parameters can be specified creating parameters with the prefix XML_POST_ and XML_GET_. In example, to pass a GET parameter of name id, the parameter must be called XML_GET_id.

    This approach is very flexible and can be used to get data from a web application like a PHP script that can create the XML data to print based on a set on input parameters passed to the report.

     


    User Feedback

    Recommended Comments

    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...