Only one row is generated in report when data sour

2019-08-28 15:06发布

问题:

I'm trying to generate report using XML data source. My input xml file has 4 lines. But JR engine generates report contains only 1st row.

I have <detail> <band> ... </band> </detail> section to get the data

My jrxml file looks like this

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport name="simpleReport">
    <queryString language="xPath"><![CDATA[/response/results]]></queryString>
    <field name="field" class="java.lang.String">
        <fieldDescription><![CDATA[//field]]></fieldDescription>
    </field>
  <field name="count" class="java.lang.String">
          <fieldDescription><![CDATA[//field[@count]]]></fieldDescription>
    </field>

    <title>
        <band height="50">
            <staticText>
          <reportElement x="0" y="0" width="180" height="15"/>
            <textElement/>
          <text><![CDATA[Report]]></text>
        </staticText>
        </band>
    </title>
  <pageHeader>
        <band/>
    </pageHeader>
  <columnHeader>
        <band height="20">
        <staticText>
          <reportElement x="180" y="0" width="180" height="20"/>
        <textElement>
            <font isUnderline="true"/>
        </textElement>
        <text><![CDATA[Event Name]]></text>
      </staticText>
      <staticText>
        <reportElement x="360" y="0" width="180" height="20"/>
        <textElement>
          <font isUnderline="true"/>
        </textElement>
        <text><![CDATA[Count]]></text>
      </staticText>
    </band>
  </columnHeader>
  <detail>
    <band height="20">
      <textField>
        <reportElement x="180" y="0" width="180" height="15"/>
        <textElement/>
        <textFieldExpression><![CDATA[$F{field}]]></textFieldExpression>
      </textField>
      <textField>
        <reportElement x="360" y="0" width="180" height="15"/>
        <textElement/>
        <textFieldExpression><![CDATA[$F{count}]]></textFieldExpression>
      </textField>
    </band>
  </detail>
  <columnFooter>
    <band/>
  </columnFooter>
  <pageFooter>
    <band height="15">
      <staticText>
        <reportElement x="0" y="0" width="40" height="15"/>
        <textElement/>
        <text><![CDATA[Page:]]></text>
      </staticText>
      <textField>
        <reportElement x="40" y="0" width="100" height="15"/>
        <textElement/>
        <textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
      </textField>
    </band>
  </pageFooter>
  <summary>
    <band/>
  </summary>
</jasperReport>

My input xmls is

<response id="1074200577">
    <results id1="0" id2="0">
        <field count="7556">one</field>
        <field count="7524">two</field>
        <field count="7402">three</field>
        <field count="7304">four</field>
    </results>
</response>

My Java client is

JasperCompileManager.compileReportToFile(inpuutjrxml, outputjasper);
JRXmlDataSource source = new JRXmlDataSource(new File(sourceFile));
HashMap<String, Object> params = new HashMap<String, Object>();
JasperPrint jasperPrint = JasperFillManager.fillReport(outputjasper, params, source);
JasperExportManager.exportReportToPdfStream(jasperPrint, new FileOutputStream(pefoutput));

How to get all values from input xml?

Also how to get the id value for ? For first xml row <field count="7556">one</field>

<fieldDescription><![CDATA[//field[@count]]]></fieldDescription>
<fieldDescription><![CDATA[//field]]></fieldDescription>

gives same data as "One"

回答1:

You can try this query (XPath):

/response/results/field

and this fields declaration:

<field name="field" class="java.lang.String">
    <fieldDescription><![CDATA[child::text()]]></fieldDescription>
</field>
<field name="count" class="java.lang.String">
    <fieldDescription><![CDATA[@count]]></fieldDescription>
</field>

Using iReport

The full jrxml file for trying from iReport:

<?xml version="1.0" encoding="UTF-8"?>
<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="xml_missing_rows" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="09291847-62d6-4f2e-bf29-6db3230ce9a4">
    <queryString language="xPath">
        <![CDATA[/response/results/field]]>
    </queryString>
    <field name="field" class="java.lang.String">
        <fieldDescription><![CDATA[child::text()]]></fieldDescription>
    </field>
    <field name="count" class="java.lang.String">
        <fieldDescription><![CDATA[@count]]></fieldDescription>
    </field>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="30be33c9-3e8c-4b8f-89b7-c2cdafd91615" x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{field}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="30be33c9-3e8c-4b8f-89b7-c2cdafd91615" x="100" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{count}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

The result for your input data will be (via preview in iReport):


Note:
I've used the iReport 5.2.0

Using Java code

You should modify your code as:

JRXmlDataSource source = new JRXmlDataSource(new File(sourceFile), "/response/results/field");
HashMap<String, Object> params = new HashMap<String, Object>();
JasperPrint jasperPrint = JasperFillManager.fillReport(outputjasper, params, source);
JasperExportManager.exportReportToPdfStream(jasperPrint, new FileOutputStream(pefoutput));

In this case we are using the JRXmlDataSource(java.io.File file, java.lang.String selectExpression) constructor. We are passing the query with data.

The jrxml should be:

<?xml version="1.0" encoding="UTF-8"?>
<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="XMLDSSample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="09291847-62d6-4f2e-bf29-6db3230ce9a4">
    <field name="field" class="java.lang.String">
        <fieldDescription><![CDATA[child::text()]]></fieldDescription>
    </field>
    <field name="count" class="java.lang.String">
        <fieldDescription><![CDATA[@count]]></fieldDescription>
    </field>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="30be33c9-3e8c-4b8f-89b7-c2cdafd91615" x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{field}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="30be33c9-3e8c-4b8f-89b7-c2cdafd91615" x="100" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{count}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

As you can see I've removed the queryString from the template - we are passing query in Java code.



回答2:

In my situation, I wrongly put the textField elements inside the summary elements. I was able to solve it by moving the textFields inside the detail elements.