溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

FineReport中怎么解析數(shù)據(jù)庫內(nèi)XML文件

發(fā)布時間:2021-07-24 17:00:42 來源:億速云 閱讀:252 作者:Leah 欄目:數(shù)據(jù)庫

本篇文章給大家分享的是有關(guān)FineReport中怎么解析數(shù)據(jù)庫內(nèi)XML文件,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

在數(shù)據(jù)庫表中,其中字段XML所存的為xml格式數(shù)據(jù)在表xmltest中。那么在使用該表進(jìn)行報表制作時,需要將存于xml字段中的值讀取出來作為報表數(shù)據(jù)源。

FineReport中怎么解析數(shù)據(jù)庫內(nèi)XML文件

XML每條記錄數(shù)據(jù)格式如下:

<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>

最終用于制作報表的數(shù)據(jù)源形式如下:

FineReport中怎么解析數(shù)據(jù)庫內(nèi)XML文件

對于這樣的情況要如何來實現(xiàn)呢?FineReport中可以通過自定義程序數(shù)據(jù)集來對xml字段數(shù)據(jù)進(jìn)行解析,最終返回所希望的數(shù)據(jù)報表。

帆軟報表FineReport的數(shù)據(jù)來源可以是任何類型的數(shù)據(jù),因此FineReport是通過AbstractTableData抽象類,也就可以用自定義類型的程序數(shù)據(jù)集,數(shù)據(jù)來源是通過把xml格式數(shù)據(jù)轉(zhuǎn)入ArrayList中。

數(shù)據(jù)集初始化方法init()

連接目標(biāo)數(shù)據(jù)庫后,執(zhí)行sql查詢語句,將xmltest表數(shù)據(jù)全部查詢出來,對于ID、NAME字段的值將直接存于新的結(jié)果集ArrayList中,對于xml字段通過GetXmlDate類對其進(jìn)行解析后再轉(zhuǎn)入ArrayList中。

GetXmlDate類代碼如下:

package com.fr.data;    
    
import java.io.InputStream;    
import java.io.InputStreamReader;    
import java.io.Reader;    
import java.util.logging.Level;    
import com.fr.base.FRContext;     
import com.fr.stable.xml.XMLReadable;  
import com.fr.stable.xml.XMLableReader;  
    
public class GetXmlDate {    
    // 定義返回值數(shù)組    
    private String[] Value = new String[3];    
    // 定義查詢的name值    
    private String[] Name = null;    
    
    protected String[] readerXMLSource(InputStream in, String[] name)    
            throws Exception {    
        Name = name;    
        InputStreamReader reader = new InputStreamReader(in, "utf-8");    
        readXMLSource(reader);    
        return Value;    
    }    
    
    protected void readXMLSource(Reader reader) throws Exception {    
        XMLableReader xmlReader = XMLableReader.createXMLableReader(reader);    
        if (xmlReader != null) {    
            xmlReader.readXMLObject(new Content());    
    
        }    
    }    
    
    private class Content implements XMLReadable {    
        public void readXML(XMLableReader reader) {    
            if (reader.isChildNode()) {    
                if (reader.getTagName().equals("Field")) {    
                    Field field = new Field();    
                    reader.readXMLObject(field);    
                    // 獲得name對應(yīng)的value值    
                    if (Name[0].equals(field.name)) {    
                        Value[0] = field.value;    
                    } else if (Name[1].equals(field.name)) {    
                        Value[1] = field.value;    
                    } else if (Name[2].equals(field.name)) {    
                        Value[2] = field.value;    
                    }    
                }    
            }    
        }    
    }    
    
    // 定義每個field的結(jié)構(gòu)    
    private class Field implements XMLReadable {    
        private String name;    
        private String type;    
        private String value;    
    
        public void readXML(XMLableReader reader) {    
            if (reader.isChildNode()) {    
                String tagName = reader.getTagName();    
                if (tagName.equals("Name")) {    
                    this.name = reader.getElementValue();    
                } else if (tagName.equals("Type")) {    
                    this.type = reader.getElementValue();    
                } else if (tagName.equals("Value")) {    
                    this.value = reader.getElementValue();    
                }    
            }    
        }    
    }    
}

定義程序數(shù)據(jù)集

定義類XMLRead.java,繼承AbstractTableData接口,實現(xiàn)getColumnCount、getColumnName、getRowCount、getValueAt四個方法;
XMLRead.java類代碼如下:

package com.fr.data;    
    
import java.io.InputStream;     
import java.io.StringBufferInputStream;  
import java.sql.Connection;    
import java.sql.DriverManager;    
import java.sql.ResultSet;    
import java.sql.Statement;    
import java.util.ArrayList;      
import com.fr.data.AbstractTableData;    
    
public class XMLRead extends AbstractTableData {    
    // 列名數(shù)組,保存程序數(shù)據(jù)集所有列名    
    private String[] columnNames = { "id", "name", "MemoryFreeSize",    
            "MemoryTotalSize", "MemoryUsage" };    
    // 保存表數(shù)據(jù)    
    private ArrayList valueList = null;    
    
    public int getColumnCount() {    
        return 5;    
    }    
    
    public String getColumnName(int columnIndex) {    
        return columnNames[columnIndex];    
    }    
    
    public int getRowCount() {    
        init();    
        return valueList.size();    
    }    
    
    public Object getValueAt(int rowIndex, int columnIndex) {    
        init();    
        return ((Object[]) valueList.get(rowIndex))[columnIndex];    
    }    
    
    private void init() {    
        // 確保只被執(zhí)行一次    
        if (valueList != null) {    
            return;    
        }    
        valueList = new ArrayList();    
        String sql = "select * from xmltest";    
        String[] name = { "MemoryFreeSize", "MemoryTotalSize", "MemoryUsage" };    
        Connection conn = this.getConncetion();    
        try {    
            Statement stmt = conn.createStatement();    
            ResultSet rs = stmt.executeQuery(sql);    
            // 用對象保存數(shù)據(jù)    
            Object[] objArray = null;    
            while (rs.next()) {    
                objArray = new Object[5];    
                String[] xmldata = null;    
                objArray[0] = rs.getObject(1);    
                objArray[1] = rs.getObject(2);    
                InputStream in = new StringBufferInputStream("<demo>"    
                        + rs.getObject(3).toString() + "</demo>");    
                GetXmlDate getxmldata = new GetXmlDate();    
                // 對xml流進(jìn)行解析,返回的為name對應(yīng)的value值數(shù)組    
                xmldata = getxmldata.readerXMLSource(in, name);    
                // 將解析后的值存于最終結(jié)果ArrayList中    
                objArray[2] = xmldata[0];    
                objArray[3] = xmldata[1];    
                objArray[4] = xmldata[2];    
                valueList.add(objArray);    
            }    
            // 釋放數(shù)據(jù)源    
            rs.close();    
            stmt.close();    
            conn.close();    
        } catch (Exception e) {    
            e.printStackTrace();    
        }    
    }    
    
    public Connection getConncetion() {    
        String driverName = "oracle.jdbc.driver.OracleDriver";    
        String url = "jdbc:oracle:thin:@192.168.100.169:1521:orcl10g";    
        String username = "temp";    
        String password = "temp123";    
        Connection con = null;    
    
        try {    
            Class.forName(driverName);    
            con = DriverManager.getConnection(url, username, password);    
        } catch (Exception e) {    
            e.printStackTrace();    
            return null;    
        }    
        return con;    
    
    }    
    
    // 釋放一些資源,因為可能會有重復(fù)調(diào)用,所以需釋放valueList,將上次查詢的結(jié)果釋放掉    
    public void release() throws Exception {    
        super.release();    
        this.valueList = null;    
    }    
}

上述代碼中的數(shù)據(jù)庫連接改動自己存檔xmltest表的數(shù)據(jù)庫

編譯程序數(shù)據(jù)源

首先編譯GetXmlDate.java然后再編譯XMLRead.java,將生成的class文件放于WEB-INF/classes/com/fr/data下。

配置程序數(shù)據(jù)源

新建報表,報表數(shù)據(jù)集>程序數(shù)據(jù)集,選擇定義好的程序數(shù)據(jù)集XMLRead.class文件,名字可以自定義,如ds1。

使用程序數(shù)據(jù)源

制作報表保存為xmlread.cpt,如下:

FineReport中怎么解析數(shù)據(jù)庫內(nèi)XML文件

BS訪問報表,效果如下:

FineReport中怎么解析數(shù)據(jù)庫內(nèi)XML文件

以上就是FineReport中怎么解析數(shù)據(jù)庫內(nèi)XML文件,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI