Search code examples
xmljasper-reports

Using xmlString parameter as actual data source for JasperReport


I am trying to provide the data to jasper report in the form of xml string as a parameter but every time i get an empty document while using jaspersoft studio. Below is the jrxml file, please help in finding the mistake, Thanks.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.18.1.final using JasperReports Library version 6.18.1-9d75d1969e774d4f179fb3be8401e98a0e6d1611  -->
<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="test2_xml" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="86062c33-1295-4207-9ed9-df0864c8981a">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="NO_DATA_ADAPTER"/>
    <parameter name="xmlString" class="java.lang.String">
        <defaultValueExpression><![CDATA["<?xml version=\"1.0\" encoding=\"UTF-8\"?><a><b><Name>val1</Name><Group>val1</Group><SomeFeild>val1</SomeFeild></b><b><Name>val2</Name><Group>val2</Group><SomeFeild>val2</SomeFeild></b></a>"]]></defaultValueExpression>
    </parameter>
    <queryString language="xPath">
        <![CDATA[a/b]]>
    </queryString>
    <field name="Name" class="java.lang.String">
        <property name="net.sf.jasperreports.xpath.field.expression" value="Name"/>
        <fieldDescription><![CDATA[Name]]></fieldDescription>
    </field>
    <field name="Group" class="java.lang.String">
        <property name="net.sf.jasperreports.xpath.field.expression" value="Group"/>
        <fieldDescription><![CDATA[Group]]></fieldDescription>
    </field>
    <field name="SomeFeild" class="java.lang.String">
        <property name="net.sf.jasperreports.xpath.field.expression" value="SomeFeild"/>
        <fieldDescription><![CDATA[SomeFeild]]></fieldDescription>
    </field>
    <columnHeader>
        <band height="31" splitType="Stretch">
            <staticText>
                <reportElement x="150" y="0" width="100" height="30" uuid="b33a123d-8987-4da4-b21b-1f9ccc50e92d"/>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement x="250" y="0" width="100" height="30" uuid="b33a123d-8987-4da4-b21b-1f9ccc50e92d"/>
                <text><![CDATA[Group]]></text>
            </staticText>
            <staticText>
                <reportElement x="360" y="0" width="100" height="30" uuid="b33a123d-8987-4da4-b21b-1f9ccc50e92d"/>
                <text><![CDATA[SomeFeild]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="150" y="0" width="100" height="30" uuid="14c51219-5ce2-47ce-abb9-71bc11a6f28c"/>
                <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="250" y="0" width="100" height="30" uuid="14c51219-5ce2-47ce-abb9-71bc11a6f28c"/>
                <textFieldExpression><![CDATA[$F{Group}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="360" y="0" width="100" height="30" uuid="14c51219-5ce2-47ce-abb9-71bc11a6f28c"/>
                <textFieldExpression><![CDATA[$F{SomeFeild}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

Solution

  • Add the following parameter to your report after the xmlString parameter. The XML_INPUT_STREAM parameter is used by the xPath query executer as input source.

    <parameter name="XML_INPUT_STREAM" class="java.io.InputStream" isForPrompting="false">
        <defaultValueExpression>new ByteArrayInputStream($P{xmlString}.getBytes("UTF-8"))</defaultValueExpression>
    </parameter>
    

    Also when you run the report in Jaspersoft Studio make sure to select "Don't use a Data Adapter" in the Preview tab.