JasperReports 3.6: Creating a Report from XML Data using XPath

Exclusive offer: get 50% off this eBook here
JasperReports 3.6 Development Cookbook

JasperReports 3.6 Development Cookbook — Save 50%

Over 50 recipes to create next-generation reports using JasperReports

$29.99    $15.00
by Bilal Siddiqui | June 2010 | Cookbooks Java Open Source

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 3.6

  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 3.6

  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 3.6

  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 3.6

  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 3.6

  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 3.6

  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 3.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 3.6

  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 3.6

JasperReports 3.6 Development Cookbook Over 50 recipes to create next-generation reports using JasperReports
Published: July 2010
eBook Price: $29.99
Book Price: $49.99
See more
Select your format and quantity:

(For more resources on JasperReports, see here.)

Creating a report from XML data using XPath

In this section, 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.

Getting ready

You need to connect your iReport installation to your XML file before starting this section. Refer to the first section of this article to learn how to connect iReport to your XML file.

How to do it...

The following steps demonstrate how to use XPath in JasperReports:

  1. Open the XMLDatasource.jrxml file from the Task3 folder of the source code download (chap4-soon to be uploaded). The Designer tab of iReport shows a report containing data in the Page Header, Detail 1, and Page Footer sections, as shown in the following screenshot:

    JasperReports 3.6

  2. Select XMLDatasource from the drop-down list just below the main menu of iReport, as shown in the following screenshot:

    JasperReports 3.6

  3. Click the Report query button on the right of the Preview tab. A Report query dialog will appear, as shown in the following screenshot:

    JasperReports 3.6

  4. You will see a Query language drop-down list at the top of the Report query tab. Select XPath as the query language from the drop-down list, as shown in the following screenshot:

    JasperReports 3.6

  5. The text area below the Query language drop-down list will split into two parts. The left part shows the actual XPath expression, whereas the right part shows XML nodes, as shown next. Expand the security node from the eventLog tree in the right part.

    JasperReports 3.6

  6. Type /eventLog//event as the XPath expression. All event nodes will appear in bold, as shown in the following screenshot:

    JasperReports 3.6

  7. Add the child nodes of the first event node as fields to the report. For this purpose, double-click on the first event node, it will expand to show its child nodes, as shown in the following screenshot:

    JasperReports 3.6

  8. Right-click on the name child of the event node. A pop-up menu will appear; select the Add node as field option. This will add name as a field in the lower-half of the Report query tab, as shown in the following screenshot:

    JasperReports 3.6

JasperReports 3.6 Development Cookbook Over 50 recipes to create next-generation reports using JasperReports
Published: July 2010
eBook Price: $29.99
Book Price: $49.99
See more
Select your format and quantity:

(For more resources on JasperReports, see here.)

  • Similarly, repeat step 8 for the occurrenceDate child of the event node. The lower-half of the Report query tab will be updated to show the two children of the event node, as shown in the following screenshot:

    JasperReports 3.6

  • Double-click on the meterheader child of the eventLog node. It will expand to show its children, as shown in the following screenshot:

    JasperReports 3.6

  • Repeat step 8 for the meterId child of the meterheader node. Finally, the lower-half of the Report query tab will show the name, occurrenceDate, and meterId fields, as shown next. Click the OK button at the bottom to dismiss the Report query dialog.

    JasperReports 3.6

  • Double-click on the Fields node in the Report Inspector window on the left of your report. The Fields node will expand to show the fields name, occurrenceDate, and meterId added in steps 8 to 11, as shown in the following screenshot:

    JasperReports 3.6

  • Drag-and-drop the meterId field from the Fields node into the Page Header section of your report. A dialog window will appear, as shown in the following screenshot; don't change anything just click the Ok button to dismiss it. A Text Field component with the expression $F{meterId} will appear in the Page Header section of your report.

    JasperReports 3.6

  • Drag-and-drop name field from the Fields node into the Detail 1 section of your report under the label named Name of Event, as shown in the following screenshot:

    JasperReports 3.6

  • Similarly, drag-and-drop occurrenceDate field from the Fields node into the Detail 1 section of your report under the label named Occurrence Date, as shown in the following screenshot:

    JasperReports 3.6

  • Switch to the Preview tab and you will see your report showing the events log fetched from the XML datasource using the XPath expression you authored in step 6.

    JasperReports 3.6

  • How it works...

    In this section you learned how to create a report when your report data source is XML.

    In steps 2 to 11 you established a connection to the XML data source, and in step 12 iReport's Report Inspector window showed the XML data as local fields (name, occurrenceDate, and meterId). Then, in steps 13 to 15, you dragged-and-dropped the local fields to design your report.

    Recall step 5 where the Report query dialog changed its view. iReport changed this dialog according to the data source used. All changes related to a data source are confined to this dialog only. The rest of the report-designing steps are mostly the dragging-and-dropping of local fields and do not depend on the type of data source.

    This feature can be referred to as loose coupling between a data source and report design. It is very useful. It makes your report design robust and independent of the type of data source used. This means you will not have to redesign the report if your data source changes.

    Summary

    iReoprt is very flexible towards XML data files. It allows you to use XPath to process and filter well-formed XML files. You can use any well-formed XML file in your report and JasperReports will process it to generate local fields, which you can drag-and-drop to design your report.


    Further resources on this subject:


    About the Author :


    Bilal Siddiqui

    Bilal Siddiqui is an Electronics Engineer, an XML consultant, and the founder of XML4Java.com, a company focused on simplifying e-business. After graduating in 1995 with a degree in electronics engineering from the University of Engineering and Technology Lahore, he began designing software solutions for industrial control systems. Later, he turned to XML and used his programming experience in C++ to build web- and WAP-based XML processing tools, server-side parsing solutions, and service applications. Bilal is a technology evangelist and a frequently published technical author.

    Bilal has been focusing exclusively on Java and XML-based open source tools and solutions since 2006. He has extensively used popular open source products such as JasperReports, ADempiere, Openbravo, and Eclipse. Bilal is a strong advocate of open source tools and is engaged not only in designing solutions based on open source tools but also collaborating with local universities in Lahore to train software and IT personnel in using open source technologies.

    Books From Packt


    iReport 3.7
    iReport 3.7

    Pentaho Reporting 3.5 for Java Developers
    Pentaho Reporting 3.5 for Java Developers

    Amazon SimpleDB Developer Guide
    Amazon SimpleDB Developer Guide

    RESTful Java Web Services
    RESTful Java Web Services

    Apache JMeter
    Apache JMeter

    Apache Geronimo 2.1: Quick Reference
    Apache Geronimo 2.1: Quick Reference

    Tomcat 6 Developer's Guide
    Tomcat 6 Developer's Guide

    JavaFX 1.2 Application Development Cookbook
    JavaFX 1.2 Application Development Cookbook


    Your rating: None Average: 4.3 (4 votes)

    Post new comment

    CAPTCHA
    This question is for testing whether you are a human visitor and to prevent automated spam submissions.
    P
    9
    P
    X
    N
    Z
    Enter the code without spaces and pay attention to upper/lower case.
    Code Download and Errata
    Packt Anytime, Anywhere
    Register Books
    Print Upgrades
    eBook Downloads
    Video Support
    Contact Us
    Awards Voting Nominations Previous Winners
    Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
    Resources
    Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software