Jump to content

passing paramter to subreport


tubamanu

Recommended Posts

Hi,

i'm using JasperReport and iReport to create an order list out of an XML File. Every order has several articles, sometimes 1 or sometimes more then 5.
 

Acutally my problem is, that my subreport displays for every order item, all artciles listet in the XML File. For example

Order1
    Article 1
    Article 2
    Article 3

Order2
    Article 1
    Article 2
    Article 3


but in xml, Order1 has only 2 Artciles an Order2 has only 1 article.
I think my problem is that theres a mistake passing the paramter from my masterreport to my subreport. But i dont find my mistake. I will add my java, jrxml and xml source file. maybe someone can have a look.....

thx!

 

Code:
Java File:import java.io.File;import java.util.HashMap;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JasperCompileManager;import net.sf.jasperreports.engine.JasperExportManager;import net.sf.jasperreports.engine.JasperFillManager;import net.sf.jasperreports.engine.JasperPrint;import net.sf.jasperreports.engine.JasperReport;import net.sf.jasperreports.engine.data.JRXmlDataSource;public class mySubTemplate {		public static void main(String[] args) {				JasperReport jasperReport;		JasperPrint jasperPrint;		HashMap<String, String> parameter = new HashMap<String, String>();        try {            JasperCompileManager.compileReportToFile("template/reportTemplate.jrxml","template/reportTemplate.jasper");             JasperCompileManager.compileReportToFile("template/reportSubTemplate.jrxml","template/reportSubTemplate.jasper");         }         catch (JRException ex) {            ex.printStackTrace();        }				try {			JRXmlDataSource xml = new JRXmlDataSource(new File("data/testData.xml"), "/list/order/packst");						jasperReport = JasperCompileManager.compileReport("template/reportTemplate.jrxml");		    jasperPrint = JasperFillManager.fillReport(jasperReport, parameter, xml);		    // pdf output		    JasperExportManager.exportReportToPdfFile(jasperPrint, "output_sub.pdf");		    		}		catch (JRException e) {		      e.printStackTrace();		}	}}

Post Edited by Manu Smith at 03/03/09 10:46
Link to comment
Share on other sites

  • Replies 11
  • Created
  • Last Reply

Top Posters In This Topic

You have to pass your parameter in your connection string for the subreport, something like this should solve your problem:

 

((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource("/list/orderarticle/article/[packstnumber = '"+$F{packstnumber}+"']")

 

Also I noticed in the subreport you have this path:

 

list/orderarticle/article/[packstnumber = '$F{packstnumber}']

 

Shouldn't you be passing the parameter in as $P{packstnumber} and not as a field? I don't have an xml file to test it on. it just looks wrong to me.


Link to comment
Share on other sites

I have also been trying to get simple master + subreport combination to work. All samples I've found at JasperForge show how to do this with older versions of iReport.

Could someone please write a concise sample of subreport implementation and put it to this forum. I believe that I'm not the only one who is trying to solve this.

Regards and thanks,

Kalevi

Link to comment
Share on other sites

thx for your help!

i don't know if it is correct to use Fields instead of paramter. I've changed subreport tag ( see Code ) but i get when i try to compile, i get these errors......

i also don't know, how to use a paramter instead of an field....:(

 

EDIT: i've taken an example from jasperreports-3.1.4demosamplesxmldatasource...
here is used a field to transfer a key date to the subreport

 

thx 4 help!

 

 

log4j:WARN No appenders could be found for logger (net.sf.jasperreports.engine.xml.JRXmlDigesterFactory).
log4j:WARN Please initialize the log4j system properly.
net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :
    Source text : ((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource("/list/orderarticle/article/[packstnumber = '"+$F{packstnumber}+"']")
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:197)
    at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:574)
    at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:542)
    at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:835)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:341)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:261)
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:275)
    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:426)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:1380)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:692)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:255)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:113)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:888)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:810)
    at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:89)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:601)
    at mySubTemplate.main(mySubTemplate.java:37)
Caused by: net.sf.jasperreports.engine.JRException: XPath selection failed. Expression: /list/orderarticle/article/[packstnumber = '1']
    at net.sf.jasperreports.engine.util.xml.XalanXPathExecuter.selectNodeList(XalanXPathExecuter.java:62)
    at net.sf.jasperreports.engine.data.JRXmlDataSource.moveFirst(JRXmlDataSource.java:263)
    at net.sf.jasperreports.engine.data.JRXmlDataSource.<init>(JRXmlDataSource.java:178)
    at net.sf.jasperreports.engine.data.JRXmlDataSource.dataSource(JRXmlDataSource.java:388)
    at report32name_1236090252694_94507.evaluate(report32name_1236090252694_94507:206)
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:186)
    ... 16 more
Caused by: javax.xml.transform.TransformerException: Nach dem Token '/' oder '//' wurde ein Positionsschritt erwartet.
    at org.apache.xpath.compiler.XPathParser.error(XPathParser.java:602)
    at org.apache.xpath.compiler.XPathParser.RelativeLocationPath(XPathParser.java:1579)
    at org.apache.xpath.compiler.XPathParser.LocationPath(XPathParser.java:1537)
    at org.apache.xpath.compiler.XPathParser.PathExpr(XPathParser.java:1259)
    at org.apache.xpath.compiler.XPathParser.UnionExpr(XPathParser.java:1178)
    at org.apache.xpath.compiler.XPathParser.UnaryExpr(XPathParser.java:1084)
    at org.apache.xpath.compiler.XPathParser.MultiplicativeExpr(XPathParser.java:1005)
    at org.apache.xpath.compiler.XPathParser.AdditiveExpr(XPathParser.java:947)
    at org.apache.xpath.compiler.XPathParser.RelationalExpr(XPathParser.java:872)
    at org.apache.xpath.compiler.XPathParser.EqualityExpr(XPathParser.java:812)
    at org.apache.xpath.compiler.XPathParser.AndExpr(XPathParser.java:776)
    at org.apache.xpath.compiler.XPathParser.OrExpr(XPathParser.java:749)
    at org.apache.xpath.compiler.XPathParser.Expr(XPathParser.java:732)
    at org.apache.xpath.compiler.XPathParser.initXPath(XPathParser.java:125)
    at org.apache.xpath.XPath.<init>(XPath.java:162)
    at org.apache.xpath.CachedXPathAPI.eval(CachedXPathAPI.java:276)
    at org.apache.xpath.CachedXPathAPI.selectNodeList(CachedXPathAPI.java:209)
    at org.apache.xpath.CachedXPathAPI.selectNodeList(CachedXPathAPI.java:189)
    at net.sf.jasperreports.engine.util.xml.XalanXPathExecuter.selectNodeList(XalanXPathExecuter.java:60)
    ... 21 more
 

 

 

Code:


Post Edited by Manu Smith at 03/03/09 14:37
Link to comment
Share on other sites

The xml code didn't work for me either so please attach the working xml file.

 

I think the path u need is: /list/orderarticle/article[packstnumber = '$F{packstnumber}']

 

This path works in my xml editor but what you had originally did not work and is one of the causes of the error you are showing. So if you attach the working xml file I'll be able to debug your parameter issue better.



Post Edited by Rodyna Zidan at 03/05/09 18:57
Link to comment
Share on other sites

  • 2 months later...

Here is a working sample of a report using a subreport element.

You have to create a new xml datasource which points to the attached xml-file.

report_main.jrxml:
You have to change the "Default Value Expression" of the "SUPREPORT_DIR" Parameter. This Parameter defines the path to your subreport. For Example "C:\\temp\\"

By the way:

The correct xPath expression for the main report is: /list/order/packst

The correct DataSource expression for feeding the subreport with values is: ((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource("/list/orderarticle/article[packstnumber = '"+ $F{packstnumber} +"']")

Have a nice day

 



Post Edited by jbo_master at 05/29/2009 07:02
Link to comment
Share on other sites

  • 2 weeks later...
  • 4 weeks later...

This example compiles and runs successfully, but it generates 0 pages.  It doesn't generate anything when supplied with a "1" or a "2" for the parameter prompt.

######### UPDATE #######

I DID get this to generate data, I had a problem in the data source I set up for the example, which I fixed

#########################

 

This is kind of a funny XML file to show for an example.  This XML file is set up with Master/Detail relationships.  Most XML files have the entire hierarchy built into each element. 

The structure of this example is:

 

<order>
     <packstr/>
     <packstr/>
     <packstr/>
</order>

<orderarticle>
     <article/>
     <article/>
     <article/>
     <article/>
</orderarticle>

 

You are much more likely to see:

<order>
    <packstr>
     <orderarticles>
         <article/>
         <article/>
         <article/>
         <article/>
    </orderarticle>
</order>

in which case I guess you'd need to provide a subDataSource rather than a dataSource.  But I don't know, because there's almost zero documentation and the Definitive Guide book examples are incorrect.  Trial and error is proving to be extremely difficult to troubleshoot this problem.  I do appreciate your effort though.

 

My main problem is that I get a runtime exception "ClassCastException" for the subdatasource I create with the following line:

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

 

I've tried pretty much everything, and it will not progress beyond that ClassCastException.



Post Edited by klingela at 07/06/2009 20:16
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...