JAXB 2.x:编组将元素值两次放入 XML

发布于 2024-10-14 17:40:18 字数 1419 浏览 3 评论 0原文

由于某种原因,元素的所有值都会被写入两次。 我的测试用例非常简单:

package test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name="root")
public class TestBean {

    private String name = null;

    @XmlElement(name="lastname")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}

然后我将文件系统中的文档编组为 XML:

    TestBean object = new TestBean();
    object.setName("abc ");
    Class<?> clazz = object.getClass();
    JAXBContext context = JAXBContext.newInstance(clazz);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    m.marshal(object, new File("test.xml"));

生成的 XML 为:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <lastname>abc abc </lastname>
</root>

为简单起见,我删除了带有命名空间定义的 package-info.java 文件。

我使用的实现是 org.eclipse.persistence.moxy 2.1.2: 包文件夹中的 jaxb.properties 文件包含此行:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

感谢您的任何提示。

For some reason, all values of an element gets written twice.
My test case is very simple:

package test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name="root")
public class TestBean {

    private String name = null;

    @XmlElement(name="lastname")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}

Then I marshall the document to the filesystem into an XML:

    TestBean object = new TestBean();
    object.setName("abc ");
    Class<?> clazz = object.getClass();
    JAXBContext context = JAXBContext.newInstance(clazz);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    m.marshal(object, new File("test.xml"));

And the resulting XML is:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <lastname>abc abc </lastname>
</root>

For simplicity I removed the package-info.java file with the namespace definitions.

The implementation I am using is org.eclipse.persistence.moxy 2.1.2:
the jaxb.properties file in the package folder contains this line:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Thanks for any hints.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

や莫失莫忘 2024-10-21 17:40:18

这是一个已知的 MOXy 问题,已在 EclipseLink 2.3.0 流中修复。可以在此处下载 EclipseLink 2.3.0:

EclipseLink 2.1.2 的解决方法是使用其他访问类型,或者使用 @XmlTransient 注释相应的字段:

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.XmlTransient;

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name="root")
public class TestBean {

    @XmlTransient
    private String name = null;

    @XmlElement(name="lastname")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

This is a known MOXy issue that has been fixed in the EclipseLink 2.3.0 stream. An EclipseLink 2.3.0 download can be obtained here:

The workaround for EclipseLink 2.1.2 is to use another access type, or to annotate the corresponding field with @XmlTransient:

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.XmlTransient;

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name="root")
public class TestBean {

    @XmlTransient
    private String name = null;

    @XmlElement(name="lastname")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
虐人心 2024-10-21 17:40:18

我尝试了你的测试,它为我提供了正确的输出:

<root>
    <lastname>abc </lastname>
</root>

它可能是 JAXB2 实现(在你的情况下为 moxy,在我的测试中则为基于本机 JDK1.6 的 JAXB2)。

I tried your test and it gives the correct output for me:

<root>
    <lastname>abc </lastname>
</root>

It could be the JAXB2 implementation (moxy in your case vs native JDK1.6 based JAXB2 for my test).

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