Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse XML files in database in FineReport

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about how to parse XML files in the database in FineReport, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

In the database table, the data stored in the field XML in xml format is in the table xmltest. Then when using this table to make a report, we need to read out the values stored in the xml field as the report data source.

The data format of each record in XML is as follows:

MemoryFreeSizeint1962

MemoryTotalSizeint2047

MemoryUsageint4

MemoryFreeSizeInt1999

MemoryTotalSizeInt2048

MemoryUsageInt10

MemoryFreeSizeInt2000

MemoryTotalSizeInt2050

MemoryUsageInt15

The final data source used to make the report is as follows:

How to achieve such a situation? In FineReport, you can parse the xml field data by customizing the program dataset, and finally return the desired data report.

Sail soft report FineReport data source can be any type of data, so FineReport is through the AbstractTableData abstract class, that is, you can use a custom type of program data set, the data source is through the xml format data into ArrayList.

Dataset initialization method init ()

After connecting to the target database, execute the sql query statement to query all the xmltest table data. The values of the ID and NAME fields will be directly stored in the new result set ArrayList, and the xml fields will be parsed by the GetXmlDate class and then transferred to ArrayList.

The GetXmlDate class code is as follows:

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 {/ / defines an array of returned values private String [] Value = new String [3] / / define the name value of the query 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) / / get the value corresponding to name 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 } / / define the structure of each field 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 ();}}

Define program dataset

Define class XMLRead.java, inherit AbstractTableData interface, and implement four methods: getColumnCount, getColumnName, getRowCount and getValueAt.

The XMLRead.java class code is as follows:

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 {/ / array of column names, save all column names of the program dataset private String [] columnNames = {"id", "name", "MemoryFreeSize", "MemoryTotalSize", "MemoryUsage"}; / / Save table data 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 () {/ / ensure that 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); / / Save data with objects 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 ("" + rs.getObject (3). ToString () + "); GetXmlDate getxmldata = new GetXmlDate () / / parse the xml stream and return an array of value values corresponding to name xmldata = getxmldata.readerXMLSource (in, name); / / store the parsed values in the final result ArrayList objArray [2] = xmldata [0]; objArray [3] = xmldata [1] ObjArray [4] = xmldata [2]; valueList.add (objArray);} / / release data source 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 } / / release some resources, because there may be repeated calls, so you need to release valueList and release public void release () throws Exception {super.release (); this.valueList = null;}}

The database connection in the above code changes the database where the xmltest table is archived.

Compiler data source

First compile GetXmlDate.java and then compile XMLRead.java, and put the generated class file under WEB-INF/classes/com/fr/data.

Configuration program data source

Create a new report, report dataset > program dataset, select the defined program dataset XMLRead.class file, the name can be customized, such as ds1.

Use program data sources

Make the report and save it as xmlread.cpt, as follows:

BS accesses the report, and the results are as follows:

The above is how to parse the XML file in the database in FineReport. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report