Parse the xml File in the Database

  • Last update:  2020-12-17
  • 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:

    https://github.com/finereport-overseas/example/blob/release/10.0/src/main/java/com/fr/data/XMLColumnNameType4Demo.java

     

    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:

    https://github.com/finereport-overseas/example/blob/release/10.0/src/main/java/com/fr/data/XMLParseDemoDataModel.java

     

    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.

    https://github.com/finereport-overseas/example/blob/release/10.0/src/main/java/com/fr/data/XMLDemoTableData.java

    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.

    1607063249900383.png

    Select XMLDemoTableData.class for Class Name, with the default value of the parameter set to "Northwind".

    1607063273235681.png

     

    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.

    1607063325241334.png

    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.

    1607063475192466.png

    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.

    https://github.com/finereport-overseas/example/blob/release/10.0/src/main/java/com/fr/data/GetXmlData.java

     

    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:

    https://github.com/finereport-overseas/example/blob/release/10.0/src/main/java/com/fr/data/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.

    企业微信截图_16010133711799.png


    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.

    1607063642322279.png

    The result is shown below.

    1607063653164183.png

     

     

     


    Attachment List


    Theme: Secondary Development
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy