Ciao a tutti.
Chiedo gentilmente una mano per capire come mai la mia insulsa java class non riesca a riempire un altrettanto insulso report prelevando i dati da un file xml.
Parto proprio da quest'ultimo che posto per farne capire la struttura.
File fatt_xml_es.xml
codice:
<?xml version="1.0" encoding="UTF-8" ?>
<fattura>
<meta>
<id_fattura>1/2014</id_fattura>
<id_cliente>001</id_cliente>
</meta>
<cliente>
<rag_sociale></rag_sociale>
<cognome>Cognome</cognome>
<nome>Nome</nome>
<indirizzo>indirizzo</indirizzo>
<cap>00000</cap>
<citta>Città</citta>
<provincia>**</provincia>
<cod_fiscale>XXXXXXXXXXXXXXXXX</cod_fiscale>
<p_iva></p_iva>
</cliente>
<prestazioni>
<prestazione>
<descrizione>Articolo 1</descrizione>
<importo>35 €</importo>
<quantita>1</quantita>
<al_iva>22%</al_iva>
</prestazione>
<prestazione>
<descrizione>Articolo 2</descrizione>
<importo>5 €</importo>
<quantita>1</quantita>
<al_iva>22%</al_iva>
</prestazione>
</prestazioni>
<totali>
<onorario>xx €</onorario>
<imponibile>yy €</imponibile>
<iva> ww €</iva>
<tot_fattura>jj €</tot_fattura>
</totali>
</fattura>
Utilizzando questo come datasource creo con iReport il file .jrxml e quindi lo compilo a .jasper. Nell'anteprima il risultato finale viene correttamente compilato.
Posto il file .jrxml
codice:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8e2812ca-ba43-4832-8e07-9f2d083bc044">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="xPath">
<![CDATA[/fattura/prestazioni/prestazione]]>
</queryString>
<field name="descrizione" class="java.lang.String">
<fieldDescription><![CDATA[descrizione]]></fieldDescription>
</field>
<field name="importo" class="java.lang.String">
<fieldDescription><![CDATA[importo]]></fieldDescription>
</field>
<field name="quantita" class="java.lang.String">
<fieldDescription><![CDATA[quantita]]></fieldDescription>
</field>
<field name="al_iva" class="java.lang.String">
<fieldDescription><![CDATA[al_iva]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="138" height="20" uuid="af0fcd3d-7cd7-4e3d-8fed-a1e4680245e6"/>
<text><![CDATA[descrizione]]></text>
</staticText>
<staticText>
<reportElement x="138" y="0" width="138" height="20" uuid="f2fb8dac-eaf9-49a0-8e64-1fec91ae5d79"/>
<text><![CDATA[importo]]></text>
</staticText>
<staticText>
<reportElement x="276" y="0" width="138" height="20" uuid="0e89c16e-da93-43da-b60e-3be4b87df3fc"/>
<text><![CDATA[quantita]]></text>
</staticText>
<staticText>
<reportElement x="414" y="0" width="138" height="20" uuid="5dfe01b8-9a19-4396-8e30-e4135b52ea37"/>
<text><![CDATA[al_iva]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="138" height="20" uuid="bdc32fd0-b572-4c49-b22f-efa7923f1f51"/>
<textFieldExpression><![CDATA[$F{descrizione}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="138" y="0" width="138" height="20" uuid="e9aaf092-81ad-457a-8f3d-0efda6f13232"/>
<textFieldExpression><![CDATA[$F{importo}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="276" y="0" width="138" height="20" uuid="6e57e4e5-d8fd-478c-b2d1-4e2f87dc48c9"/>
<textFieldExpression><![CDATA[$F{quantita}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="414" y="0" width="138" height="20" uuid="7d8f70e8-6aec-4ad0-b161-ec144623fad9"/>
<textFieldExpression><![CDATA[$F{al_iva}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
Infine posto la mia java class che prendendo il file .jasper compilato da iReport dovrebbe creare un pdf finale.
codice:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRXmlDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
public class Fatturazione {
public static void main(String[] args) {
// TODO Auto-generated method stub
JasperReport jasperReport = null;
File xml_file = null;
JRXmlDataSource dataSource = null;
JasperPrint jasperPrint = null;
try {
jasperReport = (JasperReport)JRLoader.loadObject(new File("report/report.jasper"));
Map <String, Object> param = new HashMap<String, Object>();
param.put("reportTitle", "User Report");
xml_file = new File("datasource/fatt_xml_es.xml");
dataSource = new JRXmlDataSource(xml_file);
jasperPrint = JasperFillManager.fillReport(jasperReport, param, dataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "report/report.pdf");
} catch (JRException e) {
e.printStackTrace();
}
}
}
Invece il risultante file pdf come risultato della query mostra scritto "null" in corrispondenza di tutte le colonne
. Nonostante tutti i tentativi e le ore passate su google non sono riuscito a venire a capo del problema anche perché nella compilazione non viene dato nessun problema né errore
.
Qualcuno sarebbe così gentile da suggerirmi quale possa essere il nodo della questione?
Ringraziandovi tutti per l'aiuto vi saluto.
Ciao
Matteo