使用XML文件数据源的子报表不起作用

时间:2013-01-09 17:43:05

标签: jasper-reports ireport subreport xmldatasource

我遇到了连接到XML文件的子报表的问题。当我执行报表时,预览不显示子报表数据,但如果我单独执行子报表,则会正确显示数据。我做错了什么?

数据文件:

<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>

报告:

<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>

子报表:

<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>

2 个答案:

答案 0 :(得分:2)

对于将xml文件作为数据源的子报表,您需要指定子报表的数据源。 您需要查看的属性是

  1. 连接类型:使用数据源表达式
  2. 连接/数据源表达式:
  3.   

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

    你需要类似的东西

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

答案 1 :(得分:0)

现在也许它实际上并不适合您,但是有人可以帮助您。

所有需要的内容-它为子报表设置的数据源表达式,如何实现:

<dataSourceExpression>
<![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).datasource("YOUR_SUBREPORT_XPATH") ]]>
</dataSourceExpression>
  1. 相反,“ YOUR_SUBREPORT_XPATH”使用您在子报表中设置的XPath。在这种情况下,它将是"/REPORTDATA/GRID1/line"
  2. 在较新版本的Jasper(我使用6.x)中更好,正确使用.datasource(...)代替.subDataSource(...)