【问题标题】:Only one row is generated in report when data source is XML数据源为 XML 时,报表只生成一行
【发布时间】:2013-08-08 11:18:12
【问题描述】:

我正在尝试使用 XML 数据源生成报告。我的输入 xml 文件有 4 行。但是 JR 引擎生成的报告只包含第一行。

我有<detail> <band> ... </band> </detail> 部分来获取数据

我的 jrxml 文件看起来像这样

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

我的输入 xmls 是

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

我的 Java 客户端是

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

如何从输入的 xml 中获取所有值?

另外如何获取 id 值? 对于第一个 xml 行 &lt;field count="7556"&gt;one&lt;/field&gt;

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

提供与“一”相同的数据

【问题讨论】:

    标签: java xpath jasper-reports


    【解决方案1】:

    你可以试试这个查询(XPath):

    /response/results/field
    

    还有这个字段声明:

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

    使用 iReport

    完整的 jrxml 文件,用于从 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>
    

    输入数据的结果将是(通过 iReport 中的预览):


    注意:
    我使用的是iReport 5.2.0

    使用 Java 代码

    你应该修改你的代码:

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

    在这种情况下,我们使用JRXmlDataSource(java.io.File file, java.lang.String selectExpression) 构造函数。我们正在使用数据传递查询。

    jrxml 应该是:

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

    如您所见,我已经从模板中删除了 queryString - 我们在 Java 代码中传递查询。

    【讨论】:

    • 好吧 child::text() 仍然给我null。而且@Count 也给出了 null.. 看起来我这里还有其他问题。
    • &lt;queryString language="xPath"&gt;&lt;![CDATA[/response/results]]&gt;&lt;/queryString&gt; &lt;field name="field" class="java.lang.String"&gt; &lt;fieldDescription&gt;&lt;![CDATA[//field]]&gt;&lt;/fieldDescription&gt; &lt;/field&gt; &lt;field name="count" class="java.lang.String"&gt; &lt;fieldDescription&gt;&lt;![CDATA[//field[@count]/@count]]&gt;&lt;/fieldDescription&gt; &lt;/field&gt; 这给了我one 7556 但我仍然无法获得所有属性
    • 您是否将查询更改为/response/results/field?
    • 是的。做过某事。但没有运气
    • 您是否尝试使用我的模板?
    【解决方案2】:

    在我的情况下,我错误地将 textField 元素放在了摘要元素中。我能够通过移动细节元素内的文本字段来解决它。

    【讨论】:

      猜你喜欢
      • 2014-02-07
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多