Adding a subreport Using an XML Datasource : subreport xml fields are displaying NULL in parent Report

0

I'm new to iReport (I'm using the 5.1.0 version ).

I'm trying to add a subreport to an existing parent report but when i preview it displays Null in the subreport fields.

Here's my Datasource Expression :

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

Here's the parent report XML:

<span class="tag"><parameter</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"SUBREPORT_DIR"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"java.lang.String"</span><span class="pln"> </span><span class="atn">isForPrompting</span><span class="pun">=</span><span class="atv">"false"</span><span class="tag">></span><span class="pln">
    </span><span class="tag"><defaultValueExpression></span><span class="pln"><![CDATA["C:\\Users\\Amira\\"]]>     
       </span><span class="tag"></defaultValueExpression></span><span class="pln">
</span><span class="tag"></parameter></span><span class="pln">
</span><span class="tag"><queryString</span><span class="pln"> </span><span class="atn">language</span><span class="pun">=</span><span class="atv">"xPath"</span><span class="tag">></span><span class="pln">
    <![CDATA[/projetData/projet]]>
</span><span class="tag"></queryString></span><span class="pln">
</span><span class="tag"><field</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"nomProjet"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"java.lang.String"</span><span class="tag">></span><span class="pln">
    </span><span class="tag"><fieldDescription></span><span class="pln"><![CDATA[nomProjet]]></span><span class="tag"></fieldDescription></span><span class="pln">
</span><span class="tag"></field></span><span class="pln">
</span><span class="tag"><background></span><span class="pln">
    </span><span class="tag"><band</span><span class="pln"> </span><span class="atn">splitType</span><span class="pun">=</span><span class="atv">"Stretch"</span><span class="tag">/></span><span class="pln">
</span><span class="tag"></background></span><span class="pln">
</span><span class="tag"><title></span><span class="pln">
    </span><span class="tag"><band</span><span class="pln"> </span><span class="atn">height</span><span class="pun">=</span><span class="atv">"79"</span><span class="pln"> </span><span class="atn">splitType</span><span class="pun">=</span><span class="atv">"Stretch"</span><span class="tag">/></span><span class="pln">
</span><span class="tag"></title></span><span class="pln">
</span><span class="tag"><pageHeader></span><span class="pln">
    </span><span class="tag"><band</span><span class="pln"> </span><span class="atn">height</span><span class="pun">=</span><span class="atv">"35"</span><span class="pln"> </span><span class="atn">splitType</span><span class="pun">=</span><span class="atv">"Stretch"</span><span class="tag">/></span><span class="pln">
</span><span class="tag"></pageHeader></span><span class="pln">
</span><span class="tag"><columnHeader></span><span class="pln">
    </span><span class="tag"><band</span><span class="pln"> </span><span class="atn">splitType</span><span class="pun">=</span><span class="atv">"Stretch"</span><span class="tag">/></span><span class="pln">
</span><span class="tag"></columnHeader></span><span class="pln">
</span><span class="tag"><detail></span><span class="pln">
    </span><span class="tag"><band</span><span class="pln"> </span><span class="atn">height</span><span class="pun">=</span><span class="atv">"338"</span><span class="pln"> </span><span class="atn">splitType</span><span class="pun">=</span><span class="atv">"Stretch"</span><span class="tag">></span><span class="pln">
        </span><span class="tag"><subreport></span><span class="pln">
            </span><span class="tag"><reportElement</span><span class="pln"> </span><span class="atn">uuid</span><span class="pun">=</span><span class="atv">"91594abb-0277-4441-b9dc-f6cd39cec90b"</span><span class="pln"> </span><span class="atn">x</span><span class="pun">=</span><span class="atv">"0"</span><span class="pln"> </span><span class="atn">y</span><span class="pun">=</span><span class="atv">"0"</span><span class="pln"> </span><span class="atn">width</span><span class="pun">=</span><span class="atv">"555"</span><span class="pln"> </span><span class="atn">height</span><span class="pun">=</span><span class="atv">"338"</span><span class="tag">/></span><span class="pln">
            </span><span class="tag"><dataSourceExpression></span><span class="pln"><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("//idProjet")]]></span><span class="tag"></dataSourceExpression></span><span class="pln">
            </span><span class="tag"><subreportExpression></span><span class="pln"><![CDATA[$P{SUBREPORT_DIR} + "report1_subreport1.jasper"]]></span><span class="tag"></subreportExpression></span><span class="pln">
        </span><span class="tag"></subreport></span><span class="pln">
    </span><span class="tag"></band></span><span class="pln">
</span><span class="tag"></detail></span><span class="pln">
</span><span class="tag"><columnFooter></span><span class="pln">
    </span><span class="tag"><band</span><span class="pln"> </span><span class="atn">height</span><span class="pun">=</span><span class="atv">"45"</span><span class="pln"> </span><span class="atn">splitType</span><span class="pun">=</span><span class="atv">"Stretch"</span><span class="tag">/></span><span class="pln">
</span><span class="tag"></columnFooter></span><span class="pln">
</span><span class="tag"><pageFooter></span><span class="pln">
    </span><span class="tag"><band</span><span class="pln"> </span><span class="atn">height</span><span class="pun">=</span><span class="atv">"54"</span><span class="pln"> </span><span class="atn">splitType</span><span class="pun">=</span><span class="atv">"Stretch"</span><span class="tag">/></span><span class="pln">
</span><span class="tag"></pageFooter></span><span class="pln">
</span><span class="tag"><summary></span><span class="pln">
    </span><span class="tag"><band</span><span class="pln"> </span><span class="atn">height</span><span class="pun">=</span><span class="atv">"42"</span><span class="pln"> </span><span class="atn">splitType</span><span class="pun">=</span><span class="atv">"Stretch"</span><span class="tag">/></span><span class="pln">
</span><span class="tag"></summary></span><span class="pln">
</span><span class="tag"></jasperReport></span>

 

Here's the subreport XML:

<span class="pln">    </span><span class="tag"><queryString</span><span class="pln"> </span><span class="atn">language</span><span class="pun">=</span><span class="atv">"xPath"</span><span class="tag">></span><span class="pln">
    <![CDATA[/projetData/projet]]>
</span><span class="tag"></queryString></span><span class="pln">
</span><span class="tag"><field</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"idProjet"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"java.lang.String"</span><span class="tag">></span><span class="pln">
    </span><span class="tag"><fieldDescription></span><span class="pln"><![CDATA[idProjet]]></span><span class="tag"></fieldDescription></span><span class="pln">
</span><span class="tag"></field></span><span class="pln">
</span><span class="tag"><field</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"nomProjet"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"java.lang.String"</span><span class="tag">></span><span class="pln">
    </span><span class="tag"><fieldDescription></span><span class="pln"><![CDATA[nomProjet]]></span><span class="tag"></fieldDescription></span><span class="pln">
</span><span class="tag"></field></span><span class="pln">
</span><span class="tag"><background></span><span class="pln">
    </span><span class="tag"><band</span><span class="pln"> </span><span class="atn">splitType</span><span class="pun">=</span><span class="atv">"Stretch"</span><span class="tag">/></span><span class="pln">
</span><span class="tag"></background></span><span class="pln">
</span><span class="tag"><columnHeader></span><span class="pln">
    </span><span class="tag"><band</span><span class="pln"> </span><span class="atn">height</span><span class="pun">=</span><span class="atv">"61"</span><span class="pln"> </span><span class="atn">splitType</span><span class="pun">=</span><span class="atv">"Stretch"</span><span class="tag">></span><span class="pln">
        </span><span class="tag"><staticText></span><span class="pln">
            </span><span class="tag"><reportElement</span><span class="pln"> </span><span class="atn">uuid</span><span class="pun">=</span><span class="atv">"5e05731c-782d-4bd8-8832-dbd5811bf78d"</span><span class="pln"> </span><span class="atn">x</span><span class="pun">=</span><span class="atv">"143"</span><span class="pln"> </span><span class="atn">y</span><span class="pun">=</span><span class="atv">"41"</span><span class="pln"> </span><span class="atn">width</span><span class="pun">=</span><span class="atv">"100"</span><span class="pln"> </span><span class="atn">height</span><span class="pun">=</span><span class="atv">"20"</span><span class="tag">/></span><span class="pln">
            </span><span class="tag"><textElement/></span><span class="pln">
            </span><span class="tag"><text></span><span class="pln"><![CDATA[idProjet]]></span><span class="tag"></text></span><span class="pln">
        </span><span class="tag"></staticText></span><span class="pln">
    </span><span class="tag"></band></span><span class="pln">
</span><span class="tag"></columnHeader></span><span class="pln">
</span><span class="tag"><detail></span><span class="pln">
    </span><span class="tag"><band</span><span class="pln"> </span><span class="atn">height</span><span class="pun">=</span><span class="atv">"87"</span><span class="pln"> </span><span class="atn">splitType</span><span class="pun">=</span><span class="atv">"Stretch"</span><span class="tag">></span><span class="pln">
        </span><span class="tag"><textField></span><span class="pln">
            </span><span class="tag"><reportElement</span><span class="pln"> </span><span class="atn">uuid</span><span class="pun">=</span><span class="atv">"099b70b0-6f52-4a95-843c-636c3645a319"</span><span class="pln"> </span><span class="atn">x</span><span class="pun">=</span><span class="atv">"143"</span><span class="pln"> </span><span class="atn">y</span><span class="pun">=</span><span class="atv">"0"</span><span class="pln"> </span><span class="atn">width</span><span class="pun">=</span><span class="atv">"84"</span><span class="pln"> </span><span class="atn">height</span><span class="pun">=</span><span class="atv">"18"</span><span class="tag">/></span><span class="pln">
            </span><span class="tag"><textElement/></span><span class="pln">
            </span><span class="tag"><textFieldExpression></span><span class="pln"><![CDATA[$F{idProjet}]]></span><span class="tag"></textFieldExpression></span><span class="pln">
        </span><span class="tag"></textField></span><span class="pln">
    </span><span class="tag"></band></span><span class="pln">
</span><span class="tag"></detail></span>

 

 

mannaii.amira's picture
Joined: May 20 2013 - 9:20am
Last seen: 4 years 1 week ago

1 Answer:

0

with

<strong>...$P<span style="color: #009900;">{</span>REPORT_DATA_SOURCE<span style="color: #009900;">}</span></strong></span><span style="font-family: monospace;"><span style="font-weight: bold;">...</span></span> you are using the main report´s datasource an pass it to the subreport.</p>
<p>so it has to be sth. like:</p>
<p><span class="geshifilter"><code class="java geshifilter-java">new net.<span style="color: #006633;">sf</span>.<span style="color: #006633;">jasperreports</span>.<span style="color: #006633;">engine</span>.<span style="color: #006633;">data</span>.<span style="color: #006633;">JRXmlDataSource</span><span style="color: #009900;">(new File("C:\\mycoolxmlsubreportdata")</span><span style="color: #009900;">)</span>.<span style="color: #006633;">subDataSource</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"//idProjet"</span><span style="color: #009900;">)</span></span></p>
<p>or</p>
<p><span class="geshifilter"><code class="java geshifilter-java">new net.<span style="color: #006633;">sf</span>.<span style="color: #006633;">jasperreports</span>.<span style="color: #006633;">engine</span>.<span style="color: #006633;">data</span>.<span style="color: #006633;">JRXmlDataSource</span><span style="color: rgb(0, 153, 0);">(new File("C:\\mycoolxmlsubreportdata"), </span></span><span class="geshifilter"><code class="java geshifilter-java"><span style="color: #0000ff;">"//idProjet"</span></span><span class="geshifilter"><code class="java geshifilter-java"><span style="color: #009900;">)</span></span></p>
<p>use on of the follwowing constructors to create a new datasource (on a separate XML-File) on-the-fly for your subreport:</p>
<table border="0" cellpadding="3" cellspacing="0" class="overviewSummary" summary="Constructor Summary table, listing constructors, and an explanation">
	<tbody>
		<tr class="altColor">
			<td class="colOne"><strong><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRXmlDataSource.html#JRXmlDataSource%28java.io.File%29">JRXmlDataSource</a></strong>(java.io.File file)</td>
		</tr>
		<tr class="rowColor">
			<td class="colOne"><strong><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRXmlDataSource.html#JRXmlDataSource%28java.io.File,%20boolean%29">JRXmlDataSource</a></strong>(java.io.File file, boolean isNamespaceAware)</td>
		</tr>
		<tr class="altColor">
			<td class="colOne"><strong><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRXmlDataSource.html#JRXmlDataSource%28java.io.File,%20java.lang.String%29">JRXmlDataSource</a></strong>(java.io.File file, java.lang.String selectExpression)</td>
		</tr>
		<tr class="rowColor">
			<td class="colOne"><strong><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRXmlDataSource.html#JRXmlDataSource%28java.io.File,%20java.lang.String,%20boolean%29">JRXmlDataSource</a></strong>(java.io.File file, java.lang.String selectExpression, boolean isNamespaceAware)</td>
		</tr>
	</tbody>
</table>
<p>or look here:</p>
<p>http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRXmlDataSource.html</p>
<p>Cheers, Thomas</p>
<p>http://www.thomaszimmer.net</p>

Thomas Zimmer's picture
Joined: Oct 2 2012 - 1:35am
Last seen: 1 week 5 days ago
Feedback
randomness