Search code examples
jasper-reports

Nested Tags in JasperReports


I have an XML File that looks like this:

<?xml version="1.0" encoding="utf-8"?><template>
<Kinder>
<vorname>Kevin</vorname>
<zuname>Müller</zuname>
<geburtsdatum>21.01.2010</geburtsdatum>
<Kontakt>
<typ>Vater</typ>
<vorname>Peter</vorname>
<zuname>Müller</zuname> 
</Kontakt>
<Kontakt>
<typ>Mutter</typ>
<vorname>Petra</vorname>
<zuname>Müller</zuname> 
</Kontakt>
</Kinder>
<Kinder>
<vorname>Schakkeline</vorname>
<zuname>Meyer</zuname>
<geburtsdatum>21.03.2011</geburtsdatum>
<Kontakt>
<typ>Mutter</typ>
<vorname>Maria</vorname>
<zuname>Meyer</zuname>  
</Kontakt>
</Kinder>
</template>

I want to print this in a ways that the structure is kept, like this:

Kevin Müller 21.01.2010
  Peter Müller
  Petra Müller
Schakkeline Meyer 21.03.2011
  Maria Meyer

the main lines I get like this:

 <queryString language="xPath">
    <![CDATA[/template/Kinder]]>
</queryString>
<field name="zuname" class="java.lang.String">
    <fieldDescription><![CDATA[zuname]]></fieldDescription>
</field>
<field name="vorname" class="java.lang.String">
    <fieldDescription><![CDATA[vorname]]></fieldDescription>
</field>

...

<band height="102" splitType="Stretch">
                <textField>
                    <reportElement x="32" y="34" width="136" height="16" uuid="403ac891-da84-444e-a7f1-33aef84483fa"/>
                    <textFieldExpression><![CDATA[$F{vorname}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="32" y="14" width="100" height="20" uuid="670d9156-6282-4b53-9268-275457f07ce0"/>
                    <textFieldExpression><![CDATA[$F{zuname}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="179" y="14" width="100" height="20" uuid="b7356c70-e27c-48a7-be46-e39400da4c1f"/>
                    <textFieldExpression><![CDATA[$F{geburtsdatum}]]></textFieldExpression>
                </textField>

But how do I access the nested tags like

<Kontakt>
<typ>Vater</typ>
<vorname>Peter</vorname>
<zuname>Müller</zuname> 
</Kontakt>

?

I mean something like a for-loop, in a way that I can iterate over as many subtags as exist.


Solution

  • You can use subreport with datasource query like "//Kontakt"

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

    example here community.jaspersoft.com/questions/524978/empty-subreport-xml-datasource-ireport