Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)
|
|
|
|
JasperReports - XML Data Source Sample (version 3.7.6) | ![]() |
|
|
|
Main Features in This Sample | |
| XML Data Source |
| XPath Query Executer |
|
|
Secondary Features | |
Query Executers | |
Data Sources | |
Subreports |
![]() | ![]() | ![]() | ![]() |
|
|
top | |||||
|
|||||
![]() | XML Data Source | Documented by Sanda Zaharia | |||
|
|||||
| Description / Goal |
| How to fill a report using data from an XML file. | ||
| Since |
| 0.6.0 | ||
|
|||||
|
XML Data Sources
XML documents can be used as report data sources based on appropriate JRDataSource implementations. JasperReports features a built-in XML data source implementation (JRXmlDataSource) that relies on DOM and uses XPath expressions to select data from the XML document. To instantiate an XML data source, the following inputs are required:
net.sf.jasperreports.xpath.executer.factory .
This property gives the name of an XPath executer factory class, which has to implement
the JRXPathExecuterFactory.
The XML data source provides localization support for both number and date/time values rendered as text in the wrapped XML document. In order to get the appropriate localization one have to set the following information in the JRXmlDataSource object:
java.text.DecimalFormat and java.text.SimpleDateFormat pattern syntax. If
specific patterns are not supplied, the defaults for these two format classes apply.
XML Data Source Example In our example data records are stored as node elements in the /data/northwind.xml file. The <Northwind/> root element contains two children types: <Customers/> and <Orders/> .
Below is an example of a <Customers/> entity, completely characterized by the following elements:
<Customers> <CustomerID>ALFKI</CustomerID> <CompanyName>Alfreds Futterkiste</CompanyName> <ContactName>Maria Anders</ContactName> <ContactTitle>Sales Representative</ContactTitle> <Address>Obere Str. 57</Address> <City>Berlin</City> <PostalCode>12209</PostalCode> <Country>Germany</Country> <Phone>030-0074321</Phone> <Fax>030-0076545</Fax> </Customers>And below is an example of an <Orders/> entity:
<Orders> <OrderID>10643</OrderID> <CustomerID>ALFKI</CustomerID> <EmployeeID>6</EmployeeID> <OrderDate>1997-08-25</OrderDate> <RequiredDate>1997-09-22</RequiredDate> <ShippedDate>1997-09-02</ShippedDate> <ShipVia>1</ShipVia> <Freight>29.46</Freight> <ShipName>Alfreds Futterkiste</ShipName> <ShipAddress>Obere Str. 57</ShipAddress> <ShipCity>Berlin</ShipCity> <ShipPostalCode>12209</ShipPostalCode> <ShipCountry>Germany</ShipCountry> </Orders>One can see that an <Orders> element has a <CustomerID> property which
points to the <CustomerID> element in the <Customers> node, just like
a foreign key in a relational database. One can identify a one-to-many relationship between <Customers>
and <Orders> .
In order to navigate through elements in the Northwind.xml document, an XPath query executer implementation should be specified using the net.sf.jasperreports.xpath.executer.factory property. In our case it is set in the
/src/jasperreports.properties file:
net.sf.jasperreports.xpath.executer.factory=net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory
Two JRXmlDataSource objects are necessary to store the two element types.
One of them will be populated with Customers records. In this case
record field names are given by the property names in the <Customers/> node:
JRXmlDataSource object will be populated with Orders records.
For this record type, field names are:
There are two JRXML files in the /reports directory. The CustomersReport.jrxml is the main report template. The OrdersReport.jrxml is used for generate subreports for the main report. The main report iterates through available customers in the XML data source, and prints for each of them the Customer ID, the Company Name and the own list of ship orders. The orders list exposes only the Order ID, Order Date, Ship City and Freight fields. For each customer are calculated the orders count and the total freight. In the CustomersReport.jrxml template the query language and XPath query expression are declared in the <queryString/> expression:
<queryString language="xPath"><![CDATA[/Northwind/Customers]]></queryString> The fields required for each Customer in the main report are:
<field name="CustomerID" class="java.lang.String"> <fieldDescription>CustomerID</fieldDescription> </field> <field name="CompanyName" class="java.lang.String"> <fieldDescription>CompanyName</fieldDescription> </field>The OrdersReport subreport is called when the orders list has to be completed for each customers:
<subreport> <reportElement isPrintRepeatedValues="false" x="5" y="25" width="507" height="20" isRemoveLineWhenBlank="true" backcolor="#ffcc99"/> <subreportParameter name="XML_DATA_DOCUMENT"> <subreportParameterExpression>$P{XML_DATA_DOCUMENT}</subreportParameterExpression> </subreportParameter> <subreportParameter name="XML_DATE_PATTERN"> <subreportParameterExpression>$P{XML_DATE_PATTERN}</subreportParameterExpression> </subreportParameter> <subreportParameter name="XML_NUMBER_PATTERN"> <subreportParameterExpression>$P{XML_NUMBER_PATTERN}</subreportParameterExpression> </subreportParameter> <subreportParameter name="XML_LOCALE"> <subreportParameterExpression>$P{XML_LOCALE}</subreportParameterExpression> </subreportParameter> <subreportParameter name="XML_TIME_ZONE"> <subreportParameterExpression>$P{XML_TIME_ZONE}</subreportParameterExpression> </subreportParameter> <subreportParameter name="CustomerID"> <subreportParameterExpression>$F{CustomerID}</subreportParameterExpression> </subreportParameter> <subreportExpression class="java.lang.String">"OrdersReport.jasper"</subreportExpression> </subreport>The Number pattern, Date pattern, Locale and time zone parameters above are necessary for data formatting. The CustomerID parameter is required in order to filter data in the subreport.
The <queryString/> expression in the /reports/OrdersReport.jrxml template is:
<queryString language="xPath"><![CDATA[/Northwind/Orders[CustomerID='$P{CustomerID}']]]></queryString> Only Orders with the given CustomerID are taken into account.
The only fields required from an Orders record are:
<field name="Id" class="java.lang.String"> <fieldDescription>OrderID</fieldDescription> </field> <field name="OrderDate" class="java.util.Date"> <fieldDescription>OrderDate</fieldDescription> </field> <field name="ShipCity" class="java.lang.String"> <fieldDescription>ShipCity</fieldDescription> </field> <field name="Freight" class="java.lang.Float"> <fieldDescription>Freight</fieldDescription> </field>In the /src/XmlDataSourceApp.java all necessary information is prepared to be sent at fill time: the parsed Northwind document, data formatting parameters and the CustomersReport compiled report: public void fill() throws JRException { long start = System.currentTimeMillis(); Map params = new HashMap(); Document document = JRXmlUtils.parse(JRLoader.getLocationInputStream("data/northwind.xml")); params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document); params.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, "yyyy-MM-dd"); params.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, "#,##0.##"); params.put(JRXPathQueryExecuterFactory.XML_LOCALE, Locale.ENGLISH); params.put(JRParameter.REPORT_LOCALE, Locale.US); JasperFillManager.fillReportToFile("build/reports/CustomersReport.jasper", params); System.err.println("Filling time : " + (System.currentTimeMillis() - start)); }In order to figure out more on XML data sources, just test this sample by running from the command line the ant test view command.
It will generate all supported document types containing the sample report in the /build/reports directory, and then the report will be open with JasperReports internal viewer.
|
||||
|
|||||
top | |||||
|
|||||
![]() | XPath Query Executer |
|
|||
|
|||||
| Description / Goal |
| How to fill reports using embedded XPath queries. | ||
| Since |
| 1.2.0 | ||
|
|||||
| [Under Construction] | ||||
|
|
© 2001-2010 Jaspersoft Corporation www.jaspersoft.com |