Overview
XML datasets refer to those whose data source is a series of XML files. You can use FineReport to present data and perform data analysis with these files as data sources.
Create a file frtest.xml in the path %FR_HOME%\webroot\WEB-INF\reportlets. The following figure and codes show the file content.
<?xml version="1.0" encoding="UTF-8"?>
<frtest>
<Customers>
<Name>ALFKI</Name>
<type>ALfreds Futterkiste</type>
<value>90</value>
</Customers>
<Customers>
<Name>ANATR</Name>
<type>Ana Trujillo Emparedados y helados</type>
<value>90</value>
</Customers>
</frtest>
Defining an XML Dataset
The following sections introduce how to define an XML dataset.
Click the + icon above Template Dataset and select File Dataset from the drop-down list, as shown in the following figure.
Selecting an XML File
Set File Type to XML in the file dataset popup box. Select Local File in the File Address area and click Selection to upload the existing frtest.xml file. The Key Node panel on the right is automatically refreshed to obtain all nodes in the XML file and displays the nodes in a tree structure, as shown in the following figure.
If the XML dataset has two root nodes, FineReport automatically adds an unnamed empty root node at the top.
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<author>J K.Rowing</author>
<title>HMWHERE</title>
<author>J K.Rowing</author>
<author>J K.Rowing</author>
</book>
<book>
<author>J K.Rowing</author>
<author>J K.Rowing</author>
<author>J K.Rowing</author>
<price>19</price>
</book>
</bookstore>
Selecting Nodes
The XML dataset allows all nodes in the file to be displayed in a tree structure. Different from storage processes that return multiple result sets and display each node's data in a dataset, the XML dataset only returns all data from one selected child node at a time. That is, the data ultimately displayed comes from the subordinate nodes (including child nodes and their leaf nodes) of the selected node.
<?xml version="1.0" encoding="GBK"?>
<PickBillPrintEntity>
<modifyTime>2013-08-11</modifyTime>
<exprossCode>STO</exprossCode>
<expressName>A Express</expressName>
<pickDetail>
<PrintPickDetailEntity>
<address>3746 Concord Street, Charlotte, North Carolina</address>
<city>Charlotte</city>
<contactName>Alice</contactName>
<deliveryOrderNo>668267627204339729</deliveryOrderNo>
<district>Charlotte</district>
<prov>NorthCarolina</prov>
<qty>1.0</qty>
</PrintPickDetailEntity>
<PrintPickDetailEntity>
<address>3746 Concord Street, Charlotte, North Carolina</address>
<city>Charlotte</city>
<contactName>Alice</contactName>
<deliveryOrderNo>668267627204339729</deliveryOrderNo>
<district>Charlotte</district>
<prov>North Carolina</prov>
<qty>2.0</qty>
</PrintPickDetailEntity>
</pickDetail>
<packDetail>
<PrintPackEntity>
<baocai>SJZX*1</baocai>
<itemCode>694852470667309</itemCode>
<goodDese>Telephone</goodDese>
<marksNumber>220cm*240cm</marksNumber>
<qty>1.0</qty>
<orderCount>1.0</orderCount>
<skuCount>1.0</skuCount>
</PrintPackEntity>
<PrintPackEntity>
<baocai>SJZX*1</baocai>
<itemCode>694852471667309</itemCode>
<goodDese>Computer</goodDese>
<marksNumber>220cm*240cm</marksNumber>
<qty>1.0</qty>
<orderCount>1.0</orderCount>
<skuCount>1.0</skuCount>
</PrintPackEntity>
<PrintPackEntity>
<baocai>SJZX*1</baocai>
<itemCode>694852472667309</itemCode>
<goodDese>Victoria</goodDese>
<marksNumber>200cm*230cm</marksNumber>
<qty>2.0</qty>
<orderCount>1.0</orderCount>
<skuCount>2.0</skuCount>
</PrintPackEntity>
<other>
<otherNo>1</otherNo>
</other>
</packDetail>
</PickBillPrintEntity>
Data returned to the XML dataset complies with following five rules:
If the selected node has only leaf nodes, the system takes the union of the leaf nodes as dataset fields.
If the selected node has only one child node (non-leaf node), the system takes the leaf nodes of this child node as dataset fields. For example, the node PickDetail has only one child node PrintPickDetailEntity. The data display effect when you select PickDetail is the same as that when you select PrintPickDetailEntity.
If the selected node has multiple child nodes (non-leaf nodes), the system takes the leaf nodes of the first child node as dataset fields. For example, if you select the node packDetail that has multiple child nodes (PrintPackEntity and other), the system takes the leaf nodes of the first child node PrintPackEntity as dataset fields.
If the selected node has both leaf nodes and regular child nodes (non-leaf nodes), namely, one-to-many relationship where the leaf nodes are "one" and the leaf nodes of the regular child nodes are "many", the system takes all leaf nodes and the leaf nodes of the first child node as dataset fields. The data content is displayed as follows: "One" is expanded as "many". For example, if you select the root node PickBillPrintEntity that has three leaf nodes and two child nodes (pickDetail and packDetail), the system takes the three leaf nodes and the leaf nodes of the first child node pickDetail as dataset fields.
If a leaf node has multiple values, you can separate them with semicolons (;). For example, the values of the author field are James McGovern;Per Bothner;Kurt Cagle;James Linn;Vaidyanathan Nagaraian.
For example, the parent node frtest only has one child node Customers. The returned result is the same whether you select the parent node, the child node, or the leaf nodes. Take the child node Customers as an example and click Preview, as shown in the following figure.
Effect Display
After completion, a dataset named File1 is added to Template Dataset, as shown in the following figure.
Dynamic XML Dataset
The configuration step is the same as Excel Dataset.
Remote XML Dataset
You can create remote XML datasets through two ways.
Through JSP to call XML files. The implementation method is the same as Remote Excel Dataset.
Through URL to directly call XML files.
The following sections introduce how to directly call XML files through URL.
Directly Calling XML Files Through URL
You can enter http://IP address: Port number/App name/File name.xml in URL to directly call an XML file.
For example, if the frtest.xml file used in the above example is located in the WebReport project directory in the Tomcat environment, you can enter http://localhost:8075/WebReport/frtest.xml in URL, as shown in the following figure.
Click the refresh icon on the Key Node panel to obtain data from the XML file and select the needed nodes with the same rule as the local XML file.
Notes
Characters with Unicode decimal encoding values as 183, 215, and 247 (namely, the characters "·", "×",and "÷") are not supported. If you use these values, the XML dataset will fail to be loaded.
Solution: Delete the three characters from the XML file, or wrap them as <![CDATA[xxx]]>.