Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何定义和读取空XML字段?_Xml_Jasper Reports_Jaspersoft Studio - Fatal编程技术网

如何定义和读取空XML字段?

如何定义和读取空XML字段?,xml,jasper-reports,jaspersoft-studio,Xml,Jasper Reports,Jaspersoft Studio,我正在使用Jaspersoft studio创建标签。我正在使用一个XML文件和一个XMLDataAdapter。下面是我的XML文件的一个示例 <records> <record> <metadatas> <title>first title</title> <description>descriptions</description> <key

我正在使用Jaspersoft studio创建标签。我正在使用一个XML文件和一个XMLDataAdapter。下面是我的XML文件的一个示例

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

第一个标题
描述
这里的问题是,我想访问关键字字段,在某些情况下可以设置,也可以不设置,但当我尝试读取数据适配器中的字段时,它们不会显示


是否有需要更改的配置或其他内容,我可以编译我的jasperreport。

没有特殊设置,只是没有定义与XPath查询相关的字段

在您的示例中,您可能喜欢在记录上循环,这样您的XPath查询将

<queryString language="XPath">
    <![CDATA[/records/record]]>
</queryString>
名称是任意的,类是java中值的表示,fieldDescription是与查询字符串相关的节点的相对路径

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

完整的jrxml示例

示例数据源

第一个标题
描述
第二名
说明2
测试,你好
第一个标题
描述
第二名
说明2
测试,你好
输出


正如您将要使用的,对于没有值的节点,它将显示
null
,如果您想显示空文本,只需在相对
textField
上设置
isBlankWhenNull=“true”
,没有特殊设置,您只是不定义与XPath查询相关的字段

在您的示例中,您可能喜欢在记录上循环,这样您的XPath查询将

<queryString language="XPath">
    <![CDATA[/records/record]]>
</queryString>
名称是任意的,类是java中值的表示,fieldDescription是与查询字符串相关的节点的相对路径

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

完整的jrxml示例

示例数据源

第一个标题
描述
第二名
说明2
测试,你好
第一个标题
描述
第二名
说明2
测试,你好
输出


正如您将看到的,对于没有值的节点,它将显示
null
,如果您想显示空文本,只需在相对
textField

上设置
isBlankWhenNull=“true”
,答案非常好,但我的问题是,我当时只有一条记录,但我想为多条记录创建一个标签,并且当前记录没有任何关键字,因此字段不会出现。然后手动添加?无论是作为addfield还是直接在jrmmlyea中,我都会这么做,我只是想知道是否还有其他方法,非常感谢!如果没有在xml中出现,没有自动方式,但您可以使用IDE(选择添加字段),这是一个很好的答案,但我的问题是,我当时只有一条记录,但我想为多条记录创建一个标签,而当前记录没有任何关键字,因此字段不会出现。然后手动添加它?无论是作为addfield还是直接在jrmmlyea中,我都会这么做,我只是想知道是否还有其他方法,非常感谢!如果没有xml格式,则没有自动方式,但您可以使用IDE(选择add field),欢迎使用
<?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>