我是 birt 的新手,我正在尝试使用 xml 作为数据源,但我不明白它的工作原理。 我不知道是否有必要指定更多的数据,但我没有找到任何说明使用 xml 作为数据源需要指定的内容。
我与下一个代码一起使用:
指定源和数据集的方法
/** Set data source */
void buildDataSource() throws SemanticException {
OdaDataSourceHandle dsHandle = this.efactory.newOdaDataSource("Data Source", "org.eclipse.datatools.enablement.oda.xml");
dsHandle.setProperty("FILELIST", "http://localhost:8080/BirtIntegrationImproved/ReportXmlDatasource/books.xml");
this.design.getDataSources().add(dsHandle);
}
/** Set data set */
void buildDataSet() throws SemanticException {
OdaDataSetHandle dsHandle = this.efactory.newOdaDataSet("ds","org.eclipse.datatools.enablement.oda.xml.dataSet");
dsHandle.setDataSource("Data Source");
this.design.getDataSets().add(dsHandle);
}
创建使用数据源的表
/** List of columns */
ArrayList<String> cols = new ArrayList<String>();
cols.add("id");
cols.add("author");
cols.add("title");
cols.add("genre");
cols.add("price");
buildDataSource();
buildDataSet();
TableHandle table = this.efactory.newTableItem("table", cols.size());
table.setWidth("100%");
table.setDataSet(this.design.findDataSet("ds"));
PropertyHandle computedSet = table.getColumnBindings();
ComputedColumn cs1 = null;
for( int i=0; i < cols.size(); i++) {
cs1 = StructureFactory.createComputedColumn();
cs1.setName((String)cols.get(i));
cs1.setExpression("dataSetRow[\"" + (String)cols.get(i) + "\"]");
computedSet.addItem(cs1);
}
// table header
RowHandle tableheader = (RowHandle) table.getHeader().get(0);
for(int i=0; i < cols.size(); i++) {
LabelHandle label1 = this.efactory.newLabel((String)cols.get(i));
label1.setText((String)cols.get(i));
CellHandle cell = (CellHandle) tableheader.getCells().get(i);
cell.getContent().add(label1);
}
// table detail
RowHandle tabledetail = (RowHandle) table.getDetail().get(0);
for(int i=0; i < cols.size(); i++) {
CellHandle cell = (CellHandle) tabledetail.getCells().get(i);
DataItemHandle data = this.efactory.newDataItem("data_"+(String)cols.get(i));
data.setResultSetColumn((String)cols.get(i));
System.out.println("ResultSetColumn: " + data.getResultSetColumn());
cell.getContent( ).add(data);
}
有人知道出了什么问题吗?
谢谢!!
最佳答案
要从xml作为数据源获取数据,需要按照以下步骤操作:
1. 将填充有数据的 xml 保存到系统上的任何路径。
2. 将此路径映射到数据源。
3. 将您希望在报告中看到的 xml 字段映射到数据集中。
现在可以按照与从数据库检索数据相同的方式设计报表。
关于java - 以编程方式使用 xml 作为 birt 报告中的数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21110004/