Starting from version 0.3.2, iReport provides an XML datasource that permit to fill a report reading data from an XML file.iReport 0.4.0 use JRXmlDatasource shipped with JasperReports (from version 0.6.x)

This new implementation was developed by Peter Severin (developer of JasperAssistant).This new implementation is much powerful supporting real XPath expressions. Here is what JasperReports documentation says about JRXmlDatasource.

XML data source implementation that allows to access the data from a xml
document using XPath expressions.

The data source is constructed around a node set (record set) selected
by an XPath expression from the xml document.

Each field can provide an additional XPath expresion that will be used to
select its value. This expression must be specified using the "fieldDescription"
element of the field. The expression is evaluated in the context of the current
node thus the expression should be relative to the current node.

To support subreports, sub data sources can be created. There are two different methods
for creating sub data sources. The first one allows to create a sub data source rooted
at the current node. The current node can be seen as a new document around which the
sub data source is created. The second method allows to create a sub data source that
is rooted at the same document that is used by the data source but uses a different
XPath select expression.

Generally the full power of XPath expression is available. As an example, "/A/B[@id > 0"] will select all the
nodes of type /A/B having the id greater than 0.
For a short XPath tutorial check the following URL:

This chapter cover the old iReport implementation of XML datasource. This can help to use the new one.

7.1 Configuring an XML datasource

The first step to use an XML file as a datasource is configure an XML Datasource. Select menu Datasource > Connections / Datasources, press New and select "XML file datasource" as connection type.

Fig.7.1: The XML datasource configuration panel.

Fill the name field with the name of this datasource (i.e. Adressbook), select the XML file and set the record path.

7.2 The Record Path

An XML document is structured as a tree. JasperReports need data organized as a record set, so we must present a tree as set of record. The Record Path is a string that define the minimum path
required to define a record.
Example 1:

To bind fields inside the XML, a syntax similar to XPath is used. The field path is stored in the field description (this because the field name don't support
a name like "/palette/color").
The path syntax is really simple. Is a recursive expression:

<base_path>[<symbol> <base_path>[<symbol><...>] ]

<base_path>

is the path from the root to the tag. If this path is longer than the Record Path, it's cuted to the Record Path. It is required for a field
definition. If it's alone, the value of the tag is returned.
Example: /palette/color

<symbol>

is used to add an extra path to the base path and to define what should be returned.+ add the following path to the base_path (this happen when the base_path = record path);@ return the attribute value: it's followed by the attribute name;* return all tags identified by the following path as a JRXMLDatasource

We have defined a datasource named Addressbook that use addressbook.xml as data file. The Record path is: /addressbook/category/person

Fields in the report are defined as follow:

Field Name

Field Description

Type

CATEGORY

/addressbook/category@name

java.lang.String

PERSON_ID

/addressbook/category/person@id

java.lang.String

LASTNAME

/addressbook/category/person+LASTNAME

java.lang.String

FIRSTNAME

/addressbook/category/person+FIRSTNAME

java.lang.String

HOBBIES

/addressbook/category/person+hobbies*hobby

java.lang.Object (castable to JRDataSource)

EMAIL_ADDRESSES

/addressbook/category/person*email

java.lang.Object (castable to JRDataSource)

To the master report (figure 7.2) we have added a group named CATEGORY that has as expression the category name.

Fig.7.2: The master report.

We have two subreport too, one for hobbies of a person and one for email addresses.
The first subreport element has $F{HOBBIES} as connection expression (of type datasource expression) and as subreport expression "/hobby.jasper" of type String.
The second subreport element has $F{EMAIL_ADDRESSES} as connection expression (of type datasource expression) and as subreport expression "/email.jasper" of type String.
In hobby.jasper we have defined only one field:

Field Name

Field Description

Type

HOBBY

/hobbies/hobby

java.lang.String

The same in email.jasper:

Field Name

Field Description

Type

EMAIL

/person/email

java.lang.String

Note as every field in subreport are defined specifing as root element in the path the parent element:
HOBBY -> /hobbies/hobby
EMAIL -> /person/email

JRXMLDatasource is not released under GPL license (as iReport is) but under LGPL as jasperreports: in this way you can use the JRXMLDataSource
in your program without problems.However, if you use it, please make a simbolic donation to the iReport project.