in a database table, where the fields XML is stored in XML Format data in table Xmltest. When you use this table for report authoring, you need to read the values stored in the XML field as the report data source.
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/8D/89/wKioL1ihI8mBwM4sAAAyKZTGjAM632.png "title=" 1.png "alt=" Wkiol1ihi8mbwm4saaaykztgjam632.png "/>
The data format for each record in XML is as follows:
<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>
The final data source used to make the report is as follows:
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/8D/89/wKioL1ihI9WDQAi6AABYS3DFsKI584.png "style=" float : Right; "title=" 2.png "alt=" Wkiol1ihi9wdqai6aabys3dfski584.png "/>
How is this going to be achieved? in Finereport, you can parse the XML field data by customizing the program dataset, and ultimately return the data report you want.
Sail Soft Report Finereport data source can be any type of data, so Finereport is through the Abstracttabledata abstract class, you can also use a custom type of program dataset, the data source is by the XML format data into the ArrayList.
Data Set Initialization method init ()
after connecting to the target database, execute The SQL query statement, query all the Xmltest table data, and the value of ID and Name field will be stored directly in the new result set ArrayList. The XML field is 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 return 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 of name corresponding to
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 ();
}
}
}
}
}
Defining the program Data set
Define class Xmlread.java, inherit Abstracttabledata interface, implement getColumnCount, getColumnName, GetRowCount, getvalueat four methods;
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 {
Column an array group, save all column names for 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 () {
Make sure only one time is executed
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);
Saving 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 ("<demo>"
+ Rs.getobject (3). toString () + "</demo>");
Getxmldate getxmldata = new Getxmldate ();
Parses the XML stream and returns an array of value values corresponding to name
XMLData = Getxmldata.readerxmlsource (in, name);
To save the parsed value in the final result ArrayList
OBJARRAY[2] = xmldata[0];
OBJARRAY[3] = xmldata[1];
OBJARRAY[4] = xmldata[2];
Valuelist.add (Objarray);
}
Releasing the 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 to release the results of the last query
public void Release () throws Exception {
Super.release ();
This.valuelist = null;
}
}
database connection in the above code changes the database of the Archive Xmltest table itself
Compiling the program data source
Compile First Getxmldate.java then compiles the Xmlread.java, placing the generated class file under Web-inf/classes/com/fr/data.
Configuration Program Data source
Create a new report, Report DataSet > Program DataSet, select a defined program DataSet Xmlread.class file, the name can be customized, such as DS1.
Working with program data sources
The production report is saved as xmlread.cpt, as follows:
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/8D/89/wKioL1ihJA7zRCQBAABKLCXsdlM239.png "title=" 3.png "alt=" Wkiol1ihja7zrcqbaabklcxsdlm239.png "/>
BS Access report, the effect is as follows:
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/8D/8B/wKiom1ihJB2AHOt1AAAM8BDNvR4556.png "title=" 4.png "alt=" Wkiom1ihjb2ahot1aaam8bdnvr4556.png "/>
Database Fields Data from the XML type can be converted to a report data source.
This article is from "The Bully World Blog" blog, please be sure to keep this source http://10549520.blog.51cto.com/10539520/1897220
How to parse an XML file within a database in Finereport