Home
Analytics
Using dynamic xml to generate reports at runtime
warwick.baker@gmail.com
<p>I'm the first to admit I'm rather new to Birt, but I have the requirement whereby I need to supply the xml data for a report design at runtime. I have completely failed to find a decent, complete coded exmaple of achieving this. I kinda know I need to setup a contextMap for the AppContext and add it to a run and render task, but I would really like to work from a coded example. Does anyone have one they are willing to share?</p>
<p> </p>
<p>Regards,</p>
<p>Warwick Baker.</p>
Find more posts tagged with
Magellan BI & Reporting
Comments
JFreeman
<p>I'm not sure I'm clear on exactly what you are trying to accomplish.</p>
<p> </p>
<p>Could you please provide additional details about your use case and requirement?</p>
warwick.baker@gmail.com
<p>I have a generic xml file as a datasource that I use to design a report.</p>
<p> </p>
<p>At runtime, using the birt runtime engine I want to inject the "real" xml data into to the report and generate as resultant report document based on the dynamically provided xml.</p>
<p> </p>
<p>Does that help?</p>
JFreeman
<p>So the XML file is a data source within the report design?</p>
<p>And you are wanting to dynamically modify the report designs XML at runtime with the data from the XML data source?</p>
<p> </p>
<p>Am I understanding correctly?</p>
warwick.baker@gmail.com
<p>No I don't want to modify the design at runtime. I want to "fill" the report with the data from another supplied xml file at runtime.</p>
<p> </p>
<p>The xml datasource at design has a mock set of data. I want to override that mock data with "real" data provided by another xml feed at runtime. Does this make sense?</p>
<p> </p>
<p>Kinda like jasper's model of create the design, then at runtime call up the design, fill the report with data to produce the intermediary file then render to required output.</p>
<p> </p>
<p>Thanks,</p>
<p>Warwick Baker.</p>
JFreeman
<p>When you say "fill" the report, are you just meaning with different data rows or are you wanting to use the XML to actually modify the layout of the elements within the report?</p>
<p> </p>
<p>If you are you just wanting to modify which XML file is being used as the Data Source, there are a few ways to accomplish that.</p>
<p>Are there going to differences in the number of columns and their names/types?</p>
warwick.baker@gmail.com
<p>Just different data rows. Not touching the layout or anything with the design, just different data. So if you want to say different xml datasource then yes, but it is dynamically provided at runtime via another system and will be available as an inpustream.</p>
warwick.baker@gmail.com
<p>Should have said there will be no difference to number columns or names/types etc.</p>
warwick.baker@gmail.com
<p>Figured it out, here is my basic example. It would be nice if an "official" example existed somewhere ...</p>
<pre class="_prettyXprint _lang-nocode">
public String receiveMessage(String xmlmessage) {
logger.info("Received Report Request: {}", xmlmessage);
try {
birtEngine = birtEngineFactory.getObject();
IReportRunnable runnable = null;
runnable = birtEngine.openReportDesign("C:/Users/wbaker/mars_workspace/My Reports/generic_report_template.rptdesign");
IRunAndRenderTask runAndRenderTask = birtEngine.createRunAndRenderTask(runnable);
HashMap contextMap = new HashMap();
ByteArrayInputStream is = new ByteArrayInputStream(xmlmessage.getBytes(Charset.forName("UTF-8")));
DataInputStream in = new DataInputStream(is);
contextMap.put("org.eclipse.birt.report.data.oda.xml.inputStream", in);
contextMap.put("org.eclipse.birt.report.data.oda.xml.closeInputStream", new Boolean(true));
runAndRenderTask.setAppContext(contextMap);
IRenderOption options = new RenderOption();
options.setOutputFormat("pdf");
options.setOutputFileName("C:/temp/generic_report_template_output.pdf");
PDFRenderOption pdfOptions = new PDFRenderOption( options );
pdfOptions.setOutputFormat("pdf");
pdfOptions.setOption(IPDFRenderOption.PAGE_OVERFLOW, IPDFRenderOption.FIT_TO_PAGE_SIZE);
runAndRenderTask.setRenderOption(pdfOptions);
runAndRenderTask.run();
runAndRenderTask.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
</pre>