0

I'm trying to use a XML String as a source of a report which is published in a JasperServer. First of all I'm using Jasper 6.5.1, and my application is JSF.

I have an report which receives an xPath as source, but the only way that I'd work was:

  • I create a webservice where I select the entity in database (the xml field is in this entity), so I return the Response.ok and the method produces a XML.
  • Then in the print calling I create the URL pointing to my own WS and I pass the URL as parameter net.sf.jasperreports.xml.source, but I think is a code smell because in the print calling method I already have the entity and the XML String, but I need to build an URL send to the report and the report call the webservice to get the same XML as a source.

It works, but I think is a code smell.

Reference Remote XML DataSource

So now I'm have a similar situation but I don't have the entity yet so I couldn't use that workaround, so I need to pass the XML to the report as String, as Document, as InputStream or whatever.

But it didn't work. Is there any example or way that I could do this?

I'm following this parameters possibilities: Jasper XML Constants

And I'm trying to use XML_DATA_DOCUMENT parameter:

        InputSource source = new InputSource(new StringReader(nfe.getSisXml()));

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(source);

        parameters.put("XML_DATA_DOCUMENT", document);

But I'm getting the exception:

Infinite recursion (StackOverflowError) (through reference chain: org.apache.xerces.dom.DeferredDocumentImpl["firstChild"]->org.apache.xerces.dom.DeferredElementImpl["ownerDocument"]->org.apache.xerces.dom.DeferredDocumentImpl["firstChild"]->

Thanks in advance

EDIT: After some comments I'm trying to use XML_INPUT_STREAM parameter here is a piece of my JRXML:

<queryString language="xPath">
    <![CDATA[//infNFe/det]]>
</queryString>
<field name="NfeId" class="java.lang.String">
    <fieldDescription><![CDATA[/nfeProc/NFe/infNFe/@Id]]></fieldDescription>
</field>
<field name="NfeVersao" class="java.lang.String">
    <fieldDescription><![CDATA[/nfeProc/NFe/infNFe/@versao]]></fieldDescription>
</field>

The XML_INPUT_STREAM parameter doesn't appear in JRXML and if I edit the JRXML and add it and save jasper exclude the tags.

Here is a Screenshot of my parameters

I'm trying to sending my XML string as InputStream like this:

InputStream inputStream = new ByteArrayInputStream(nfe.getSisXml().getBytes(Charset.forName("UTF-8")));
parameters.put("XML_INPUT_STREAM", inputStream);

And I'm using my own client where I convert parameters using Jackson in JSON before sending to report in server:

final ObjectNode requestNode = JsonNodeFactory.instance.objectNode()
            .put("reportUnitUri", path)
            .put("async", false)
            .put("freshData", false)
            .put("saveDataSnapshot", false)
            .put("outputFormat", "pdf")
            .put("ignorePagination", false);
            //.put("pages", "0");

    if (parameters != null && !parameters.isEmpty()) {
        final ArrayNode parametersNode = requestNode.putObject("parameters").putArray("reportParameter");

        for (Map.Entry<String, Object> e : parameters.entrySet()) {
            final ObjectNode parameterNode = parametersNode.addObject()
                    .put("name", e.getKey());

            if (e.getValue() instanceof String) {
                parameterNode.putArray("value").add((String) e.getValue());
            } else {
                parameterNode.putArray("value").add("" + new ObjectMapper().convertValue(e.getValue(), JsonNode.class));
            }
        }
    }
Guilherme Bernardi
  • 490
  • 1
  • 6
  • 18
  • 1
    My answer [here](https://stackoverflow.com/a/37441567/5882963) might help. I pass the XML string as parameter and construct the `XML_INPUT_STREAM` parameter based on it, inside the JRXML. If this doesn't help please post some sample JRXML and XML data to work with. – Narcis Oct 16 '18 at 08:35
  • @Narcis I checked your answer, in Java you pass the XML as String or input stream? And after that do you change something in the report? Because in my jasper version 6.5.1 the XML_INPUT_STREAM parameter is disabled for any change. – Guilherme Bernardi Oct 16 '18 at 13:53
  • It is up to you what you pass in from Java code, but passing only one of them makes sense in that case. In that answer you have a complete workflow to test it in JasperServer via the REST_v2 API by passing in the `xmlString` parameter. What do you mean by "the XML_INPUT_STREAM parameter is disabled for any change"? – Narcis Oct 16 '18 at 14:17
  • I'm trying to use the XML_INPUT_STREAM parameter which according with [link](http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/query/JRXPathQueryExecuterFactory.html#XML_INPUT_STREAM) receive an Input Stream, so I just need to send like this: InputStream inputStream = new ByteArrayInputStream(nfe.getSisXml().getBytes(Charset.forName("UTF-8"))); parameters.put("XML_INPUT_STREAM", inputStream); ? I don't have to change anything in report? Because the XML_INPUT_STREAM parameter doesn't allow any change. – Guilherme Bernardi Oct 16 '18 at 14:29
  • Make sure you don't use a data adapter as it might override your parameters. Anyway, posting a sample JRXML might help figuring this out. – Narcis Oct 16 '18 at 14:34
  • @Narcis I edited the original question with some better information, let me know if you don't understand. – Guilherme Bernardi Oct 16 '18 at 15:09
  • Are you making a REST call from your app to JasperServer? Is so, there’s no point in passing an InputStream or a Document as parameters if you already have the XML String – Narcis Oct 16 '18 at 16:22
  • @Narcis Yes, I'm making a json rest call, are you saying that I could pass the xml as a string and convert it in xPath inside of my report? – Guilherme Bernardi Oct 16 '18 at 17:28
  • 1
    Well, that's what the answer I pointed you to was about. – Narcis Oct 17 '18 at 08:38

0 Answers0