问题描述
我正在使用Sun的JDK 1.6.0_18中的JAXB 2.1.10:
I'm using JAXB 2.1.10 from Sun's JDK 1.6.0_18:
D:\ apps \ _jdk160_18 \ bin> xjc.exe -version
xjc版本JDK 6中的JAXB 2.1.10
用于XML绑定的JavaTM体系结构(JAXB)参考实现,(在JDK 6中构建JAXB 2.1.10)
D:\apps\jdk160_18\bin>xjc.exe -versionxjc version "JAXB 2.1.10 in JDK 6"JavaTM Architecture for XML Binding(JAXB) Reference Implementation, (build JAXB 2.1.10 in JDK 6)
我需要让JAXB的marshaller生成一个空元素(例如< someStringField>< / someStringField>
或< someStringField / >
)当JAXB对象具有空字符串的值()时。但是,JAXB并没有这样做,而是从输出中省略了元素(就好像它是一个可选元素)。
I need to have JAXB's marshaller produce an empty element (e.g. <someStringField></someStringField>
or <someStringField/>
) when the JAXB object has the value of the empty string (""
). However, rather than doing that, JAXB omits the element altogether from its output (as if it where an optional element).
我在Internet上的搜索表明JAXB应该正常创建此标记,只要将字段设置为非空的空字符串(即myJAXBObject.setSomeStringField();):
My searches in the Internet indicated that JAXB should normally create this tag, as long as you set the field to the non-null empty string (i.e. myJAXBObject.setSomeStringField(""); ):
在我的XSD中,我试图表明(我知道的每一种方式)元素的存在是强制性的即使它是空的:
In my XSD, I've tried to indicate (in every way I know) that the element's presence is mandatory even if it is empty:
<xs:element name="outerElement">
<xs:complexType>
<xs:sequence>
<xs:element name="someStringField" type="xs:string" nillable="false" minOccurs="1" />
<xs:element name="someOtherStringField" type="xs:string" />
生成的代码如下所示(两个元素都相同):
The generated code looks like this (it's the same for both elements):
@XmlElement(name = "someStringField", required = true)
protected String someStringField;
@XmlElement(name = "someOtherStringField", required = true)
protected String someOtherStringField;
然而,当我编组以下对象时......
However, when I marshal the following object...
outerElement.setSomeStringField("");
outerElement.setSomeOtherStringField("Value was set");
我得到:
<outerElement>
<someOtherStringField>Value was set</someOtherStringField>
</outerElement>
当我期待:
<outerElement>
<someStringField></someStringField>
<someOtherStringField>Value was set</someOtherStringField>
</outerElement>
或者:
<outerElement>
<someStringField/>
<someOtherStringField>Value was set</someOtherStringField>
</outerElement>
有人能发现我做错了吗?
Can anyone spot what I'm doing wrong?
推荐答案
感谢您提出此问题()引起我们的注意。该错误已得到修复,将包含在EclipseLink 2.1.1维护版本中。如果你想更早地访问这个修补程序,你可以从7月8日开始每晚下载:
Thank you for bringing this issue (https://bugs.eclipse.org/319028) to our attention. The bug has been fixed and will be included in the EclipseLink 2.1.1 maintenance release. If you want access to this fix earlier you can pick up the nightly download starting July 8th from:
- http://www.eclipse.org/eclipselink/downloads/nightly.php
这篇关于JAXB:空字符串不会产生空元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!