寻找JasperStudio xml子数据源的工作示例 [英] Looking for a working example of JasperStudio xml subdatasource

查看:323
本文介绍了寻找JasperStudio xml子数据源的工作示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里和Jasper一起搜索过,找不到使用xml子数据源的可下载的工作示例。我理解有关必须演员等问题,但我不熟悉Jasper,知道我做错了什么,各处都有各种各样的音符。所以,如果我的XML看起来像......

I have scoured here and Jasper and cannot find a downloadable, working example of using an xml subdatasource. I understand the issues regarding having to cast and such but am not familiar enough with Jasper to know what I am doing wrong what with various notes all over the place. So if I had XML that looked like...

<a>
    <b>
    <c>
       <d>
       <e>
    </c>
    <f>
</a>

我可以轻松地在/ a上提供一个提供b和f的父报告。但是我使用subdatasource(on / a / c)构建子报告的所有尝试都失败了。如果有人有一个非常简单和工作,它将非常感激,因为我真的无法在网上找到一个工作的例子 - 甚至Jasper 6.2示例代码是使用不同的机制构建的。

I can easily make a parent report on /a that provides b and f. But all my attempts to build a sub report using subdatasource (on /a/c) have failed. If anyone has one that is pretty simple and works it would be much appreciated because I seriously cannot find a working example on the web - even the Jasper 6.2 sample code is built using a different mechanism.

推荐答案

要将subDataSource传递给子报表,需要向其添加数据源表达式。

To pass a subDataSource to a subreport, you need to add a Data Source Expression to it.

这个表达式通常是对JRXmlDataSource的一个API方法的调用: dataSource([...]) subDataSource([...] ),如javadoc中所述: http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRXmlDataSource.html#method_summary

This expression is usually a call to one of the API methods of the JRXmlDataSource: dataSource([...]) or subDataSource([...]) as described in the javadoc here: http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRXmlDataSource.html#method_summary

您可以手动或在JasperSoft Studio(JSS)的帮助下添加/编辑数据源表达式。在这两种情况下,假设您已经在迭代XML中的a类型节点。

You can add/edit a Data Source Expression either manually or with the help of JasperSoft Studio (JSS). In both cases, the assumption is that you are already iterating over "a" type nodes from your XML.

使用JSS时,只需编辑子报表属性数据源表达式使用此表达式:

When working with JSS, just edit the Subreport property "Data Source Expression" with this expression:

((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE})
.subDataSource("/c")

如果导入,表达式可以更短报告的高级属性中的报表中的类,通常在此路径上:杂项>进口

The expression can be shorter if you import the class in your report from within report's advanced properties, usually on this path: Misc > Imports

如果您选择手动执行此操作,那么您应该在JRXML:

If you choose to manually do this, then you should have something resembling this in your JRXML:

<jasperReport ...>
  <import value="net.sf.jasperreports.engine.data.JRXmlDataSource"/>
  ...

  <subreport>
    <reportElement .../>
    <dataSourceExpression><![CDATA[
      ((JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/c")
    ]]></dataSourceExpression>
    <subreportExpression><![CDATA["path/to/subreport"]]></subreportExpression>
  </subreport>

  ...
</jasperReport>

通过几次修改,您可以让JasperReports xmldatasource示例使用子数据源:

With a couple of modifications you can have the JasperReports xmldatasource sample work with a sub data source:

步骤#1。确保CustomersReport.jrxml仅包含子报表部分的此代码:

Step #1. Make sure that CustomersReport.jrxml contains only this code for the subreport part:

<subreport>
      <reportElement isPrintRepeatedValues="false" x="5" y="25" width="507" height="20" isRemoveLineWhenBlank="true" backcolor="#FFCC99" uuid="e7de82f1-2e1c-4459-bef3-307e57903e0b"/>
      <dataSourceExpression><![CDATA[
        ((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource("/Northwind/Orders[CustomerID='" + $F{CustomerID} + "']")
      ]]></dataSourceExpression>
      <subreportExpression><![CDATA["OrdersReport.jasper"]]></subreportExpression>
</subreport>

第2步。在OrdersReport.jrxml中,请确保:

Step #2. In OrdersReport.jrxml make sure that:


  • 删除CustomerID参数

  • 您的XPATH查询看起来像这样:< queryString language =xPath><![CDATA [Orders]]>< / queryString>

  • you remove the CustomerID parameter
  • your XPATH query looks like this: <queryString language="xPath"><![CDATA[Orders]]></queryString>

重要提示:
在这种情况下,我使用了JRXmlDataSource的 dataSource(expr)方法因为客户和订单是XML文件中的兄弟姐妹。
如果订单是每个客户(嵌套在客户中),我可能会使用 subDataSource(Orders)方法。

Important note: In this case, I used the dataSource(expr) method of the JRXmlDataSource because the Customers and Orders are siblings in the XML file. If Orders had been per Customer(nested within Customers) I would have probably used the subDataSource("Orders") approach.

这篇关于寻找JasperStudio xml子数据源的工作示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆