Java Reference
In-Depth Information
JRXmlDataSource xmlDataSource = new JRXmlDataSource(
new BufferedInputStream(getServletConfig().getServletContext()
.getResourceAsStream("/reports/AircraftData.xml")),
"/AircraftData/aircraft");
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), xmlDataSource);
response.setContentType("application/pdf");
servletOutputStream.flush();
servletOutputStream.close();
}
catch (Exception e)
{
// display stack trace in the browser
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
response.setContentType("text/plain");
response.getOutputStream().print(stringWriter.toString());
}
}
}
As can be seen in the example above, we need to pass the XML document and an
XPath expression to the constructor of JRXmlDataSource . The example assumes that
we saved the XML file, shown in the beginning of this section, as AircraftData.
xml . In this particular case, we chose to pass the XML document as an input stream.
JRXmlDataSource contains other constructors that allow us to send the XML
document as an org.w3c.dom document, as a java.io file, or as a String containing
a Uniform Resource Identifier (URI) . Passing an XPath expression is optional. If we
don't pass one, the datasource will be created from all the sub-elements of the root
element in the XML file. However, if we do pass one, then the datasource will be
created from all the elements inside the XPath expression.
Custom Datasources
So far we've seen all of the JRDataSource implementations provided by
JasperReports. If we need to extract data from a type of datasource not directly
supported by JasperReports, we can create a class implementing JRDataSource to
meet our needs. In this section, we will create a CsvDataSource class, which will
allow us to create reports from Comma Separated Value (CSV) files.
 
Search WWH ::




Custom Search