Question

I'm having a problem with subreport connected to a XML file. When I execute the report the preview don't show the subreport data, but if I execute the subreport alone, it shows the data correctly. What am I doing wrong?

Data File:

<REPORTDATA>
<HEAD>
    <NUMBER>1</NUMBER>
    <DESC>BLA BLA</DESC>
    <DATE>2012.01.01</DATE>
</HEAD>
<GRID1>
    <line id="1">
        <NUMBER>11</NUMBER>
        <DESC>g11BLA BLA</DESC>
        <DATE>2012.01.01</DATE>
    </line>
    <line id="2">
        <NUMBER>12</NUMBER>
        <DESC>g12BLA BLA</DESC>
        <DATE>2012.01.01</DATE>
    </line>
</GRID1>
<GRID2>
    <line id="1">
        <NUMBER>21</NUMBER>
        <DESC>g21BLA BLA</DESC>
        <DATE>2012.01.01</DATE>
    </line>
    <line id="2">
        <NUMBER>22</NUMBER>
        <DESC>g22BLA BLA</DESC>
        <DATE>2012.01.01</DATE>
    </line>
</GRID2>

Report:

<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="reporttest" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="6a9eed56-d1c3-46d8-9e16-4166c146b8a3">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["C:\\Users\\A2O\\Desktop\\"]]></defaultValueExpression>
</parameter>
<queryString language="xPath">
    <![CDATA[/REPORTDATA/HEAD]]>
</queryString>
<field name="NUMBER" class="java.lang.String">
    <fieldDescription><![CDATA[NUMBER]]></fieldDescription>
</field>
<field name="DESC" class="java.lang.String">
    <fieldDescription><![CDATA[DESC]]></fieldDescription>
</field>
<field name="DATE" class="java.lang.String">
    <fieldDescription><![CDATA[DATE]]></fieldDescription>
</field>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="79" splitType="Stretch">
        <textField>
            <reportElement uuid="bd7929b3-092d-44b9-b488-c0418fb57749" x="4" y="6" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{NUMBER}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement uuid="de902808-67e8-4b08-bd0b-c1d735409843" x="6" y="32" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{DESC}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement uuid="86d57bd0-01d3-4b1f-b9bb-0eb7cf2c19fc" x="5" y="56" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{DATE}]]></textFieldExpression>
        </textField>
    </band>
</title>
<pageHeader>
    <band height="13" splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band height="12" splitType="Stretch"/>
</columnHeader>
<detail>
    <band height="26" splitType="Stretch">
        <staticText>
            <reportElement uuid="819f4027-8ad0-46b4-b07c-c80234fe8411" x="12" y="0" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[Static text]]></text>
        </staticText>
    </band>
</detail>
<columnFooter>
    <band height="11" splitType="Stretch"/>
</columnFooter>
<pageFooter>
    <band height="9" splitType="Stretch"/>
</pageFooter>
<summary>
    <band height="42" splitType="Stretch">
        <subreport>
            <reportElement uuid="4ab29d23-9ead-491e-8ca3-8f9790280026" x="0" y="0" width="555" height="42"/>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "reporttest_subreport1.jasper"]]></subreportExpression>
        </subreport>
    </band>
</summary>

Subreport:

<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="reporttest_subreport1" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="ca109af7-4e08-405d-b43f-4062115c500e">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="xPath">
    <![CDATA[/REPORTDATA/GRID1/line]]>
</queryString>
<field name="NUMBER" class="java.lang.String">
    <fieldDescription><![CDATA[NUMBER]]></fieldDescription>
</field>
<field name="DESC" class="java.lang.String">
    <fieldDescription><![CDATA[DESC]]></fieldDescription>
</field>
<field name="DATE" class="java.lang.String">
    <fieldDescription><![CDATA[DATE]]></fieldDescription>
</field>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band splitType="Stretch"/>
</title>
<pageHeader>
    <band height="36" splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band height="69" splitType="Stretch">
        <staticText>
            <reportElement uuid="867a24dd-6172-40ef-be33-f8e51fa21207" x="0" y="0" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[Grid1 data:]]></text>
        </staticText>
        <staticText>
            <reportElement uuid="0885dada-a149-4cb7-b82c-fdfa9847061d" x="32" y="49" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[NUMBER]]></text>
        </staticText>
        <staticText>
            <reportElement uuid="7261c001-6771-47dc-9749-5e6f64fb86df" x="159" y="49" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[DESC]]></text>
        </staticText>
        <staticText>
            <reportElement uuid="2b95c6fc-3fd9-4df1-89c6-5249173db3e1" x="287" y="49" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[DATE]]></text>
        </staticText>
    </band>
</columnHeader>
<detail>
    <band height="23" splitType="Stretch">
        <textField>
            <reportElement uuid="becea2ba-40fd-4208-8191-c7475293ed5b" x="32" y="1" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{NUMBER}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement uuid="182efb3b-6a66-4634-be35-fe03e8a0cd45" x="159" y="0" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{DESC}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement uuid="bdb70a35-b4e6-4857-96c5-f607ee59bc07" x="287" y="0" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{DATE}]]></textFieldExpression>
        </textField>
    </band>
</detail>
<columnFooter>
    <band height="15" splitType="Stretch"/>
</columnFooter>
<pageFooter>
    <band height="6" splitType="Stretch"/>
</pageFooter>
<summary>
    <band height="7" splitType="Stretch"/>
</summary>

Was it helpful?

Solution

For sub report with the xml files as a datasource, you need to specify the datasource for the sub report. Properties you need to have a look at are

  1. Connection Type : Use a Datasource Expression
  2. Connection / Datasource Expression :

((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("//YOUR_SUBREPORT_ENTITY").

You will need something similar to this

<dataSourceExpression><![CDATA[

((net.sf.jasperreports.engine.data.JRXmlDataSource)
$P{REPORT_DATA_SOURCE}).subDataSource("//YOUR_SUBREPORT_ENTITY") ]]>
</dataSourceExpression>

OTHER TIPS

Maybe now its not actually for you, but someone it help.

All what need - its set datasource expression for subreport, how it realise:

<dataSourceExpression>
<![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).datasource("YOUR_SUBREPORT_XPATH") ]]>
</dataSourceExpression>
  1. Instead "YOUR_SUBREPORT_XPATH" use XPath that you set in subreport. In this case it will be "/REPORTDATA/GRID1/line"
  2. In newer versions of Jasper (i used 6.x) is better and correct use .datasource(...) instead .subDataSource(...)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top