如何使用 JAXB 将空元素解组为空字符串
有一个像这样的伪代码:
Alma alma = new Alma();
alma.setKorte(""); //Korte is a string member
marshaller.marshal(alma, stringwriter);
System.out.println(stringwriter.toString());
它产生的输出(我知道这是空元素存在的某种技巧,但这就是它在我的系统中的工作原理,所以我之前的人已经这样设置了) :
<alma><korte/></alma>
这对我来说很好。但是当我解组它时,空字符串没有正确解组,但 korte 将为空。如何使 jaxb 将空元素解组为空字符串?
我使用JDK6捆绑的jaxb。
编辑:
alma 类看起来像(类名已更改,但它是这样的):
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Alma", propOrder = {
"korte"
})
public class Alma
implements Serializable
{
private final static long serialVersionUID = 100L;
@XmlElement(required = true)
protected String korte;
There is a pseudo code like this:
Alma alma = new Alma();
alma.setKorte(""); //Korte is a string member
marshaller.marshal(alma, stringwriter);
System.out.println(stringwriter.toString());
And it produces the output of (I know this is some kind of trick that the empty element is there, but this is how it works in my system, so someone before me have set this like this):
<alma><korte/></alma>
Which is fine for me. But when I unmarshal it, the empty string is not unmarshalled correctly, but korte will be null. How to make jaxb to unmarshal the empty element into empty string?
I use JDK6 bundled jaxb.
EDIT:
The alma class looks like (name of class is changed, but it is like this):
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Alma", propOrder = {
"korte"
})
public class Alma
implements Serializable
{
private final static long serialVersionUID = 100L;
@XmlElement(required = true)
protected String korte;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 String 属性,JAXB 实现应将空元素解组为“”。解决方案是升级到包含此修复的 JAXB 实现的较新版本。
下面的示例对我使用 JDK 1.6.0_20 中包含的 JAXB 版本和 EcliseLink JAXB (MOXy) 有效2.3。
演示
输出
Alma
JAXB implementations should unmarshal empty elements as "" for String properties. The solution will be to upgrade to a newer version of your JAXB implementation that contains this fix.
The example below worked for me using the version of JAXB included in JDK 1.6.0_20 and EcliseLink JAXB (MOXy) 2.3.
Demo
Output
Alma