How to create report

0

Hi

Now I use XML file as datasource,the XML is as below:

    <riders>
        
    <rider>
            <riderCode>TAP006-R1</riderCode>
            <riderName>rider1</riderName>
            <riderDescription/>
            <benefits><benefit><code>HRS1-0</code><label>benefit1</label><price>包含 Yes</price></benefit>
            <benefit><code>HRS1-2</code><label>benefit2</label><price>包含 Yes</price></benefit></benefits>
     </rider>
         <rider>
            <riderCode>TAP006-R2</riderCode>
            <riderName>rider2</riderName>
            <riderDescription/>
            <benefits><benefit><code>HRS1-3</code><label>benefit3</label><price>包含 Yes</price></benefit>
            <benefit><code>HRS1-4</code><label>benefit4</label><price>包含 Yes</price></benefit></benefits>
     </rider>
     </riders>
 
I want to create a report displayed as below:
 
rider1
benefit1
benefit2
 
rider2
benefit3
benefit4
 
And how I should do?
Thank you very much
 
2765629348's picture
Joined: Sep 19 2016 - 10:55pm
Last seen: 2 years 11 months ago

1 Answer:

0

Hi,

For Parent data -:

Main report Datasource (xpath)  =  /riders/rider

in Main report Group= $F{riderCode}

 

For Child data-:

You can use "List" Component in details band, creating one more dataset inside report which will be SubCategory of XML data.

 

Below is the sample working fine-: (Create/use xml data adapter, same as XML mentioned in question)

Output-:

 

 

Jrxml Code -:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.1.0.final using JasperReports Library version 6.0.2  -->
<!-- 2016-09-21T16:55:21 -->
<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="SampleXMLsource" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4959202e-c791-4e25-83cc-e1199cff9a69">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="SampleXML"/>
<import value="net.sf.jasperreports.engine.data.JRXmlDataSource"/>
<import value="net.sf.jasperreports.engine.JRDataSource"/>
<subDataset name="Grp1" uuid="9c262504-1bbe-4430-9bdb-633382643914">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="SampleXML"/>
<queryString language="xPath">
<![CDATA[/riders/rider/benefits/benefit]]>
</queryString>
<field name="code_1" class="java.lang.String">
<fieldDescription><![CDATA[code]]></fieldDescription>
</field>
<field name="label_1" class="java.lang.String">
<fieldDescription><![CDATA[label]]></fieldDescription>
</field>
<field name="price_1" class="java.lang.String">
<fieldDescription><![CDATA[price]]></fieldDescription>
</field>
</subDataset>
<queryString language="xPath">
<![CDATA[/riders/rider]]>
</queryString>
<field name="riderCode_1" class="java.lang.String">
<fieldDescription><![CDATA[riderCode]]></fieldDescription>
</field>
<field name="riderName_1" class="java.lang.String">
<fieldDescription><![CDATA[riderName]]></fieldDescription>
</field>
<field name="riderDescription_1" class="java.lang.String">
<fieldDescription><![CDATA[riderDescription]]></fieldDescription>
</field>
<group name="rider">
<groupExpression><![CDATA[$F{riderCode_1}]]></groupExpression>
<groupHeader>
<band height="15">
<textField>
<reportElement mode="Opaque" x="0" y="0" width="215" height="15" backcolor="#F0E6E6" uuid="d5ea8186-2f14-494b-9629-dcdacaabf2bd"/>
<box leftPadding="10"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font fontName="Calibri" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{riderName_1}]]></textFieldExpression>
</textField>
</band>
</groupHeader>
<groupFooter>
<band height="21"/>
</groupFooter>
</group>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="25" splitType="Stretch">
<componentElement>
<reportElement x="35" y="0" width="315" height="25" uuid="d76b1167-69d3-47fa-8526-9ba5e849818c"/>
<datasetRun subDataset="Grp1" uuid="f4054377-5bba-4959-a926-0d75181db891">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/rider/benefits/benefit")]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="25" width="315">
<textField>
<reportElement x="5" y="1" width="100" height="20" uuid="78f03b07-07ab-4f1a-9795-9eee4293a433"/>
<textElement verticalAlignment="Middle">
<font fontName="Calibri" size="11"/>
</textElement>
<textFieldExpression><![CDATA[$F{code_1}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="110" y="1" width="100" height="20" uuid="77c5486e-e2d8-4bc2-ab04-10d00e34a83f"/>
<textElement verticalAlignment="Middle">
<font fontName="Calibri" size="11" isBold="true" isUnderline="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{label_1}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="215" y="1" width="100" height="20" uuid="3b2ccadc-aa7c-4445-90ce-7fe660d2e1d7"/>
<textElement verticalAlignment="Middle">
<font fontName="Calibri" size="11"/>
</textElement>
<textFieldExpression><![CDATA[$F{price_1}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>
 

 
Regards
Ajay
ajay_1311's picture
160
Joined: May 17 2012 - 11:27pm
Last seen: 1 month 4 days ago
Feedback