user2797499
user2797499

Reputation: 21

How to generate an XML report?

I have a requirement where I need to generate the XML reports and I need to populate the the XML with the values from Database.

I starting using Jasper Reports where I can generate the reports of PDF, xls and CSV but I couldn't able to generate the XML.

I have a jrxml file which I did compile and used the snippet below for generating PDF, CSV and XLS, but generating XML seems to be an issue:

if(contentType == 'text/csv'){
    params.response.setHeader("Content-disposition", "attachment;filename=" + params.reportName + ".csv")
    exporter = new net.sf.jasperreports.engine.export.JRCsvExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    if(params.feed){
        exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER, "|");
    }
    fileName = reportTempDir + "/" + fileName + ".csv"
}else if(contentType== 'application/pdf'){
    params.response.setHeader("Content-disposition", "attachment;filename=" + params.reportName + ".pdf")
    exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    fileName = reportTempDir + "/" + fileName + ".pdf"
}else{
    params.response.setHeader("Content-disposition", "attachment;filename=" + params.reportName + ".xls")
    exporter = new net.sf.jasperreports.engine.export.JExcelApiExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE,true);
    exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET ,false);
    exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS ,true);
    exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS ,true);
    exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND ,false);
    exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_GRAPHICS ,false);
    exporter.setParameter(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN ,false);
    exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_CELL_BORDER ,false);
    exporter.setParameter(JRXlsExporterParameter.IS_FONT_SIZE_FIX_ENABLED ,true);
    exporter.setParameter(JRXlsExporterParameter.SHEET_NAMES ,[sheetName]as String[]);
    fileName = reportTempDir + "/" + fileName + ".xls"
}
FileOutputStream osGenre = new FileOutputStream(fileName);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, osGenre);
exporter.exportReport();

Upvotes: 2

Views: 3518

Answers (1)

Petter Friberg
Petter Friberg

Reputation: 21710

There is the JRXmlExporter

JRXmlExporter exporter  = new JRXmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleXmlExporterOutput(outputSteam));
exporter.exportReport();

This will generate an xml file of your JasperPrint, however its not a clean extraction of your data but only a xml representation of the the print

Example

<?xml version="1.0" encoding="UTF-8"?>
<jasperPrint xmlns="http://jasperreports.sourceforge.net/jasperreports/print" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/print http://jasperreports.sourceforge.net/xsd/jasperprint.xsd" name="ReportStat" pageWidth="595" pageHeight="842" topMargin="30" leftMargin="20" bottomMargin="30" rightMargin="20" locale="it_IT" timezone="Europe/Berlin">
    <origin band="title"/>
    <origin band="columnHeader"/>
    <origin band="detail"/>
    <origin band="summary"/>
    <page>
       <text verticalAlignment="Middle" textHeight="12.578125" lineSpacingFactor="1.2578125" leadingOffset="-2.1972656">
            <reportElement uuid="dfe13f55-12a6-4c33-b5ba-00dd61f37c96" mode="Opaque" x="20" y="335" width="100" height="20" forecolor="#000000" backcolor="#CCCCCC" origin="1" srcId="2" printId="1"/>
            <box leftPadding="2">
                <topPen lineWidth="0.25"/>
                <leftPen lineWidth="0.25"/>
                <bottomPen lineWidth="0.25"/>
                <rightPen lineWidth="0.25"/>
            </box>
            <textContent><![CDATA[TOTALE]]></textContent>
        </text>
        ....
    </page>
</jasperPrint>

If you need a more clean extraction of data you need to create your own CustomExporter, a good start can be to override the JRCsvExporter and implement your own logic.

Upvotes: 2

Related Questions