Jump to content
We've recently updated our Privacy Statement, available here ×

JasperReports 6.3.1 - Passing an external XML DataSource


arorejudo_1
Go to solution Solved by narcism,

Recommended Posts

Hi All,

I have defined a Report containing two subreports using a XML Document Data Adapter

We have assigned a stacit XML File into File/URL field.

When we execute preview functionality everything worked OK,

Then I have devoloped a java class in order to send a XML to build the report

 

private void generateReport(){
        Document doc = null;
        String jasperFileName = "D:/myDir/Report.jasper";
        String jasperPDFFileName = "D:/test.pdf";
        File file = new File("D:/myDir/resources/XMLTest.xml");
        try
        {
                JRXmlDataSource dataSource = new JRXmlDataSource(file);
                JasperPrint jprint = (JasperPrint) JasperFillManager.fillReport(jasperFileName, null, dataSource);
                JasperExportManager.exportReportToPdfFile(jprint, jasperPDFFileName);
        }
        catch(Exception e){
                 e.printStackTrace();
        }
}
 
The problem is report is generated using original XML file defined into JasperReports Studio
 
Could anyone help me with this.
 
Thanks in advance.
Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

  • Solution

If you are using the same XML document in your main report and subreports, it may happen that your XML document does not reach the subreports, which may have already been compiled with JSS and contain the original XML location. You should remove that property/properties, recompile and use the following approach.

A similar case is described in the JasperReports xmldatasource sample(http://jasperreports.sourceforge.net/sample.reference/xmldatasource/index.html#xmldatasource):

- the XML document is parsed and passed as parameter(see the fill() method used in the sample: https://github.com/Jaspersoft/jasperreports/blob/jr-6-3-1/jasperreports/demo/samples/xmldatasource/src/XmlDataSourceApp.java#L99)

- then, inside the main JRXML, the same parameter is passed onto the subreport to avoid parsing the document again(see the main report from the sample: https://github.com/Jaspersoft/jasperreports/blob/jr-6-3-1/jasperreports/demo/samples/xmldatasource/reports/CustomersReport.jrxml#L52)

- you may need to consider using the date/number patterns, locale and timezone parameters if your XML contains data related to these items

Link to comment
Share on other sites

Thank you very much!

Now report generated are using XML  file passed as parameter but now I have two new problems:

1.- XML is generated using BusinessWorks including namespace prefix for each field and JasperReport get an error for this prefix

      If I remove prefix in jrxml file and test file if works

2.- I'm using a table on each subreport and they aren't shown at all.

     They was showd correctly in preview mode from Jasper Report Studio

Now jrxml is like this:

Main Report:

<subreport>
     <reportElement x="-11" y="14" width="544" height="106" uuid="af0452a6-c845-4edb-a704-cc98aae8c98d"/>
          <subreportParameter name="PARAM1">
               <subreportParameterExpression><![CDATA[$F{field1}]]></subreportParameterExpression>
          </subreportParameter>
          <subreportParameter name="XML_DATA_DOCUMENT">
                <subreportParameterExpression><![CDATA[$P{XML_DATA_DOCUMENT}]]></subreportParameterExpression>
          </subreportParameter>
          <dataSourceExpression>
               <![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/")]]>
          </dataSourceExpression>
         <subreportExpression><![CDATA["Subreport1.jasper"]]></subreportExpression>
</subreport>
 
SubReport Table:
 
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
     <datasetRun subDataset="DatosPersonalCivil" uuid="5a185d29-333b-4e30-a4a1-c850c051d077">
           <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
     </datasetRun>
     <jr:column width="20" uuid="6feb0f56-1192-40c6-8a31-4f57e121009c">

...

Could you help me with this problem?

Thanks in advance

 

Link to comment
Share on other sites

For XML namespace support you need to parse the document with the namespace aware flag (http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/util/JRXmlUtils.html#parse(java.io.File,%20boolean)). In the mentioned sample that would look like:

Document document = JRXmlUtils.parse(JRLoader.getLocationInputStream("data/northwind.xml"), true);[/code]

 

A side note: if you are subDataSource-ing for the subReport, you don't have to pass the XML_DATA_DOCUMENT

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