【问题标题】:How to define and read empty XML fields?如何定义和读取空的 XML 字段?
【发布时间】:2017-09-27 13:27:47
【问题描述】:

我正在使用 Jaspersoft Studio 创建标签。我正在使用 XML 文件和 XMLDataAdapter。这是我的 XML 文件的示例

<records>
  <record>
    <metadatas>
        <title>first title</title>
        <description>descriptions</description>
        <keywords />
        <!-- ... -->
    </metadatas>
  </record>
</records>

这里的问题是我想访问关键字字段,在某些情况下可以设置或不设置,但是当我尝试读取数据适配器中的字段时,它们没有显示出来。

是否有要更改的配置或我可以编译我的 jasperreport 的东西。

【问题讨论】:

    标签: xml jasper-reports jaspersoft-studio


    【解决方案1】:

    没有特殊设置,您只是没有定义相对于 XPath 查询的字段

    在您的示例中,您可能希望循环记录以便您的 XPath 查询将是

    <queryString language="XPath">
        <![CDATA[/records/record]]>
    </queryString>
    

    要定义指向keywords 的字段,请使用fieldDescription

    <field name="keywords" class="java.lang.String">
        <fieldDescription><![CDATA[metadatas/keywords]]></fieldDescription>
    </field>
    

    名称是任意的,类是java中值的表示,fieldDescription是与你的queryString相关的节点的相对路径

    JasperSoft 工作室

    在 JasperSoft Studio 中,这是通过首先defining your data adapter,然后右键单击报告节点并选择“数据集和查询”来实现的。在对话框中选择您的数据适配器和 XPath,然后编写您的 XPath 查询并单击要添加为字段的字段

    完整的 jrxml 示例

    <?xml version="1.0" encoding="UTF-8"?>
    <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="XMLTest" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b487729d-4510-4485-b838-19c491042208">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="XMLTest"/>
        <queryString language="XPath">
            <![CDATA[/records/record]]>
        </queryString>
        <field name="title" class="java.lang.String">
            <fieldDescription><![CDATA[metadatas/title]]></fieldDescription>
        </field>
        <field name="description" class="java.lang.String">
            <fieldDescription><![CDATA[metadatas/description]]></fieldDescription>
        </field>
        <field name="keywords" class="java.lang.String">
            <fieldDescription><![CDATA[metadatas/keywords]]></fieldDescription>
        </field>
        <columnHeader>
            <band height="20" splitType="Stretch">
                <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                <staticText>
                    <reportElement x="0" y="0" width="170" height="20" uuid="3ce3004b-8324-4f57-ba9f-77cdffc711da"/>
                    <textElement verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[title]]></text>
                </staticText>
                <staticText>
                    <reportElement x="170" y="0" width="170" height="20" uuid="6ee7c571-ecdc-45cc-b11d-600099121301"/>
                    <textElement verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[description]]></text>
                </staticText>
                <staticText>
                    <reportElement x="340" y="0" width="210" height="20" uuid="111fa52f-bc0b-4698-8ba9-a7af03988254"/>
                    <textElement verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[keywords]]></text>
                </staticText>
            </band>
        </columnHeader>
        <detail>
            <band height="20" splitType="Stretch">
                <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                <textField>
                    <reportElement x="0" y="0" width="170" height="20" uuid="444010e8-18ce-4594-a1b1-ca3d120b091d"/>
                    <textElement verticalAlignment="Middle">
                        <font isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="170" y="0" width="170" height="20" uuid="9a27a707-c5df-4905-ac21-e42b21c1d77c"/>
                    <textElement verticalAlignment="Middle">
                        <font isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{description}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="340" y="0" width="210" height="20" uuid="1621de6d-7200-49aa-a0d4-56f64fec1b91"/>
                    <textElement verticalAlignment="Middle">
                        <font isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{keywords}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
    </jasperReport>
    

    示例数据源

    <records>
      <record>
        <metadatas>
            <title>first title</title>
            <description>descriptions</description>
            <keywords />
        </metadatas>
      </record>
      <record>
        <metadatas>
            <title>second title</title>
            <description>descriptions 2</description>
            <keywords>test, hello</keywords>
        </metadatas>
      </record>
    </records><records>
      <record>
        <metadatas>
            <title>first title</title>
            <description>descriptions</description>
            <keywords />
        </metadatas>
      </record>
      <record>
        <metadatas>
            <title>second title</title>
            <description>descriptions 2</description>
            <keywords>test, hello</keywords>
        </metadatas>
      </record>
    </records>
    

    输出

    如您所见,它将为没有值的节点显示null,如果您想显示空文本,只需在相对textField 上设置isBlankWhenNull="true"

    【讨论】:

    • 很好的答案,但我的问题是我当时只有一条记录,但我想为多条记录创建一个标签,而当前记录没有任何关键字,所以字段没有出现。
    • 然后手动添加?作为添加字段或直接在 jrxml 中
    • 是的,这就是我要做的,我只是想知道是否还有其他方法,
    • 非常感谢!
    • 如果xml中不存在则没有自动方式,但您可以使用IDE(选择添加字段),欢迎
    猜你喜欢
    • 2012-01-23
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 2012-01-14
    • 2022-01-14
    相关资源
    最近更新 更多