In this recipe, 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.
You need to connect your iReport installation to your XML file before starting this recipe. Refer to the Connecting to XML datasource recipe of this chapter to learn how to connect iReport to your XML file.
XMLDatasource.jrxml
file from the Task3
folder of the source code for this chapter. 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: /eventLog//event
as the XPath expression. All event nodes will appear in bold, as shown in the following screenshot: $F{meterId}
will appear in the Page Header section of your report. 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:In this recipe 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.
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.
18.119.103.96