如何使用 JAXB 将空元素解组为空字符串

发布于 2024-11-16 07:19:21 字数 754 浏览 8 评论 0原文

有一个像这样的伪代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卷耳 2024-11-23 07:19:21

对于 String 属性,JAXB 实现应将空元素解组为“”。解决方案是升级到包含此修复的 JAXB 实现的较新版本。

下面的示例对我使用 JDK 1.6.0_20 中包含的 JAXB 版本和 EcliseLink JAXB (MOXy) 有效2.3

演示

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Alma.class);

        String xmlString = "<alma><korte/></alma>";
        StringReader xmlReader = new StringReader(xmlString);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Alma alma = (Alma) unmarshaller.unmarshal(xmlReader);

        System.out.println(alma.getKorte().length());
    }

}

输出

0

Alma

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@XmlType(name = "Alma", propOrder = { "korte" })
public class Alma implements Serializable {

    private final static long serialVersionUID = 100L;

    @XmlElement(required = true)
    protected String korte;

    public String getKorte() {
        return korte;
    }

    public void setKorte(String korte) {
        this.korte = korte;
    }

}

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

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Alma.class);

        String xmlString = "<alma><korte/></alma>";
        StringReader xmlReader = new StringReader(xmlString);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Alma alma = (Alma) unmarshaller.unmarshal(xmlReader);

        System.out.println(alma.getKorte().length());
    }

}

Output

0

Alma

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@XmlType(name = "Alma", propOrder = { "korte" })
public class Alma implements Serializable {

    private final static long serialVersionUID = 100L;

    @XmlElement(required = true)
    protected String korte;

    public String getKorte() {
        return korte;
    }

    public void setKorte(String korte) {
        this.korte = korte;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文