I. Overview
The article demonstrates how to build class datasets with XML files, which are generally categorized into two types, namely, XML files in file systems and databases, based on the storage system of XML files.
II. XML Files in File Systems
1. Scenarios
FineReport allows developers to parse the XML data via custom class datasets, acquire the XML filename dynamically through parameters, and use the file as a report data source eventually.
The example XML is shown below.
<?xml version="1.0" encoding="UTF-8"?>
<Northwind>
<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>
</Northwind>
2. Define the Encapsulated Class XMLColumnNameType4Demo
First, specify the parameters "name" and "type" for other classes to call and use. Please see here for detailed codes:
3. Define the XMLParseDemoDataModel Class
The XMLParseDemoDataModel class, extending from interface AbstractDataModel, implements four methods, namely, getColumnCount, getColumnName, getRowCount and getValueAt. Please refer to the following URL for detailed codes:
4. Define the Class Dataset XMLDemoTableData
To dynamically display XML file contents with the $filename parameter, place the XML file into a directory, and define the need-to-be-parsed data columns. The data column names defined here are matched with field names in the XML file. Please refer to the following URL for code examples.
Note: if you are using XML files in different structures, please modify the variable COL_DEEP to the corresponding value of node-level where column names are stored.
5. Compile the Class Dataset
Complie XMLColumnNameType4Demo.java, XMLParseDemoDataModel.java and XMLDemoTableData.java respectively, and place the resulting class files into %FR_HOME%\webapps\webroot\WEB-INF\classes\com\fr\data.
Note: XML file paths need to be modified accordingly. For example, "D://" for files on D: drive.
6. Configure the Class Dataset
Click on "Class" as shown below.
Select XMLDemoTableData.class for Class Name, with the default value of the parameter set to "Northwind".
7. Use the Class Dataset
Click the Preview icon on the left side, and input the name of the need-to-be-displayed XML file in the opened dialog for parameters. Then, click OK to extract and convert data from Northwind.xml into a report data source. Please see below.
III. XML Files in Databases
Let's say we have a database table "xmltest", with data in XML format stored in the field "XML" as shown below. To make a report with this table, the first thing is to extract values stored in the field "XML" to use as its data source.
The field contains the following entries:
<Field><Name>MemoryFreeSize</Name><Type>int</Type><Value>1962</Value></Field> <Field><Name>MemoryTotalSize</Name><Type>int</Type><Value>2047</Value></Field> <Field><Name>MemoryUsage</Name><Type>int</Type><Value>4</Value></Field>
<Field><Name>MemoryFreeSize</Name><Type>Int</Type><Value>1999</Value></Field> <Field><Name>MemoryTotalSize</Name><Type>Int</Type><Value>2048</Value></Field> <Field><Name>MemoryUsage</Name><Type>Int</Type><Value>10</Value></Field>
<Field><Name>MemoryFreeSize</Name><Type>Int</Type><Value>2000</Value></Field> <Field><Name>MemoryTotalSize</Name><Type>Int</Type><Value>2050</Value></Field> <Field><Name>MemoryUsage</Name><Type>Int</Type><Value>15</Value></Field>
1. Define the Class Dataset
Connect to the target database, and execute SQL queries to extract all the data from table "xmltest". Values of field "ID" and "NAME" are directly stored into the new result set ArrayList. Those in "XML" are parsed by GetXmlData class and transferred to ArrayList then. For detailed information on GetXmlData, please refer to the following URL.
Next, define the class XMLRead.java, extending from the interface AbstractTableData, to implement the following methods, namely, getColumnCount, getColumnName, getRowCount and getValueAt. Please go to the URL for the complete code of XMLRead.java:
Note: please change the database connection to the actual one where you store the table "xmltest"
2. Compile the Class Dataset
Compile GetXmlData.java first, and XMLRead.java then. Afterward, place the resulting class files into WEB-INF/classes/com/fr/data.
3. Use the Class Dataset
Here we leave out the process of the class dataset configuration, and go straight to using the XML class dataset to make a report.
The result is shown below.