Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

JasperReports 3.6: Creating a Report from XML Data using XPath

Save for later
  • 180 min read
  • 2010-06-24 00:00:00

article-image

XML is a popular data source used in many applications. JasperReports allows you to generate reports directly from XML data. This first section of the article teaches you how to connect iReport to an XML file stored on your PC. In the second section of this article by Bilal Siddiqui, author of JasperReports 3.6 Development Cookbook, you will create a report from data stored in an XML file. In order to process an XML file and extract information from it, JasperReports uses XPath, which is a popular query language to filter XML data. So you will also learn how to use XPath expressions for report generation.

(For more resources on JasperReports, see here.)

Connecting to an XML datasource

XML is a popular data source used in many applications. JasperReports allows you to generate reports directly from XML data.

This section teaches you how to connect iReport to an XML file stored on your PC.

Getting ready

You need an XML file that contains report data. The EventsData.xml file is contained in the source code download (chap4). Unzip the source code file for this article (chap:4) and copy the Task2 folder from the unzipped source code to a location of your choice.

How to do it...

  1. Run iReport; it will open showing a Welcome Window, as shown in the following screenshot:

    jasperreports-36-creating-report-xml-data-using-xpath-img-0

  2. If you have not made any database connection so far in your iReport installation, you will see an Empty datasource shown selected in a drop-down list just below the main menu. Click on the Report Datasources icon shown encircled to the right of the drop-down list in the screen-shot shown below:

    jasperreports-36-creating-report-xml-data-using-xpath-img-1

  3. A new window named Connections / Datasources will open, as shown below. This window lists an Empty datasource as well as the data sources you have made so far.

    jasperreports-36-creating-report-xml-data-using-xpath-img-2

    Unlock access to the largest independent learning library in Tech for FREE!
    Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
    Renews at €14.99/month. Cancel anytime
  4. Click the New button at the top-right of the Connections / Datasources window. This will open a new Datasource selection window, as shown in the following screenshot:

    jasperreports-36-creating-report-xml-data-using-xpath-img-3

  5. Select XML file datasource from the datasources type list. Click Next.
  6. A new window named XML file datasource will open, as in the following screenshot:

    jasperreports-36-creating-report-xml-data-using-xpath-img-4

  7. Enter XMLDatasource as the name for your new connection for the XML datasource in the text box beside the Name text field, as shown in the following screenshot:

    jasperreports-36-creating-report-xml-data-using-xpath-img-5

  8. Click the Browse button beside the XML file text box to browse to the EventsData.xml file located in the Task2 folder that you copied in the Getting ready section. Click the Open button, as shown in the following screenshot:

    jasperreports-36-creating-report-xml-data-using-xpath-img-6

  9. Select the Use the report XPath expression when filling the report option in the XML file datasource window, as shown in the following screenshot:

    jasperreports-36-creating-report-xml-data-using-xpath-img-7

  10. Leave the other fields at their default values. Click the Test button to test the new XML datasource connection. You will see a Connection test successful message dialog.
  11. Click the Save button to save the newly created connection. A Connections / Datasources window will open showing your new XML datasource connection set as the default connection in the connections list, as shown highlighted in the following screenshot:

    jasperreports-36-creating-report-xml-data-using-xpath-img-8