Remote XML Datasource

Summary

This article descries how to use xpath2 to retrieve a remote XML web-service and publish it on JasperReports Server. This article assumes you are using JasperReports Server 4.7 or above

iReport

iReport already contains support for xpath2 and remote XML datasources. To configure a new remote XML datasource just create a new "Remote XML File Datasource" and set the query executor as xpath2. Writing the xpath2 query is beyond the scope of this document, however, you can take a look at this related article: Declare a field for XML datasource

JasperReports Server Preparation

By default JasperReports Server does not ship with the xpath2 query executer, for this we'll need a jar file that comes with iReport.

  1. Copy jasperreports-extensions-3.5.3.jar from /ireport/modules/ext to JasperReports Server/jasperserver-pro/WEB-INF/lib/jasperreports-extensions-3.5.3.jar (or if using Jaspersoft Studio plugins/com.jaspersoft.studio.data_5.6.0.final.jar and plugins/com.jaspersoft.studio_5.6.0.final.jar)
  2. Add the following line to /jasperserver-pro/WEB-INF/classes/jasperreports.properties

    net.sf.jasperreports.query.executer.factory.xpath2=com.jaspersoft.jrx.query.JRXPathQueryExecuterFactory

  3. Restart Tomcat

Report Unit

Now you can upload your report unit containing a valid xpath2 query. The key is uploading the report and setting it to Do not link a data source - the remote XML data source is easy enough to use, you simply need a parameter called $P{XML_URL} which contains the URL of an XML feed, for example:

https://api.twitter.com/1/statuses/user_timeline.xml?include_entities=true&include_rts=true&screen_name=jaspersoft&count=200"

Parameters

You can parametrize the xpath2 query as you wish, this is simple within iReport. If you want to parametrize the URL that you are calling, for example, changing the screen_name then you need to declare two parameters (note that the order in which they are declared is critical)

Parameter 1: $P{twitter_account} Used for asking the user, in my example asking them the user name

Parameter2: $P{XML_URL} Used for passing the URL to the report, in my example the default value expression is set to concatenate $P{twitter_account} to the URL like this: 

https://api.twitter.com/1/statuses/user_timeline.xml?include_entities=true&include_rts=true&screen_name=" + $P{twitter_account} + "&count=200

SubReport

This example contains a sub-report as well. You can upload the sub-report as usual and keep this in mind: You will want to "Use a datasource expression" that looks like this one:

((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource("//statuses/status[id='" + $F{id} + "']/entities/user_mentions/user_mention")

No need to declare any special parameters in the sub-report, it just works!

Debugging

You can add net.sf.jasperreports.engine as DEBUG (as superuser) to http://localhost:8080/jasperserver-pro/log_settings.html - you should get some stats about the datasource if it fails.

Sample Reports

IMPORTANT NOTE: Twitter has deprecated their 1.0 API which this sample was built upon, unfortunately the sample will need to be re-written to work with the 1.1+ API and this report does not currently work. (written February 2014)

This zip file contains two JRXML files, you will need to create Input Controls to get them to work, I suggest getting the Complete Sample instead

  • Note that the sub-report expression is set to repo: report67.jrxml - change this as you wish

Complete Sample (repository export):

This is a more complete sample and uses parametrization. The sample should execute upon being imported with the js-import.sh tool, for example, run the command: ./js-import.sh --input-zip /path/to/remote_xml_sample.zip - remember to install the xpath2 query executor first! The sample will be found in /Reports/Samples/Remote XML Sample

Screenshot

What's a sample without a of good screenshot?

Feedback
randomness