简体   繁体   English

如何从 XML 文件 (JRC) Java 设置 Crystal Report DataSource

[英]How to set Crystal Report DataSource from XML file (JRC) Java

I am using Crystal Reports for Eclipse, and I want to open a report and set the DataSource from XML files and then print it.我正在为 Eclipse 使用 Crystal Reports,我想打开一个报表并从 XML 文件中设置数据源,然后打印它。

As far as I know, the Schema and Data have to be set in a XML dataset for it to work, but I haven't managed to make it work yet.据我所知,模式和数据必须设置在 XML 数据集中才能正常工作,但我还没有设法让它工作。

ReportClientDocument report = new ReportClientDocument();
report.open("reportpath.rpt",0)

byte[] schemaXML = Files.ReadAllBytes(new File("schema.xsd").toPath());
ByteArray schemaByteArray = new ByteArray(schemaByteArray);

byte[] dataXML = Files.ReadAllBytes(new File("data.xml").toPath());
ByteArray xmlByteArray = new ByteArray(xmlByteArray);

DatabaseController dbc = report.getDatabaseController();
XMLDataSet dataset = new XMLDataSet();
dataset.setXMLSchema(schemaByteArray);
dataset.setXMLData(xmlByteArray);

dbc.setDataSource(dataset,"TableName", "TableName");

The report exports a blank PDF file.报告导出一个空白 PDF 文件。 No error at runtime.运行时没有错误。 I'm thinking that maybe I got the XSD and XML files wrong, any sample is appreciated.我在想,也许我得到了 XSD 和 XML 文件错误,任何样本都值得赞赏。

For anyone who stumbles upon this question: The code is working fine.对于任何偶然发现这个问题的人:代码运行良好。 The problem lies with the XML and XSD files.问题在于 XML 和 XSD 文件。 I got them to work with one single element, and I am still trying to figure out how to make it work with a DataSet.我让他们使用单个元素,但我仍在尝试弄清楚如何使其与 DataSet 一起使用。

One correction in the last line of code:最后一行代码的一个更正:

dbc.setDataSource(dataset,"", "");

This way, it will automatically try to find the schema in the XML file这样,它会自动尝试在 XML 文件中查找架构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM