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 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.
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.
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.
Run your report, the result should be similar to the one shown in the following picture.
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.