使用 xmlString 参数作为 JasperReport 的实际数据源

Using xmlString parameter as actual data source for JasperReport

我试图以 xml 字符串的形式将数据作为参数提供给 jasper 报告,但每次我在使用 jaspersoft studio 时得到一个空文档。 下面是jrxml文件,请帮忙找错, 谢谢

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

xmlString 参数之后将以下参数添加到您的报告中。 XML_INPUT_STREAM 参数被 xPath 查询执行器用作输入源。

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

此外,当您 运行 Jaspersoft Studio 中的报告时,请确保 select 在“预览”选项卡中选择“不要使用数据适配器”。