JAXB 2.x:编组将元素值两次放入 XML
由于某种原因,元素的所有值都会被写入两次。 我的测试用例非常简单:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个已知的 MOXy 问题,已在 EclipseLink 2.3.0 流中修复。可以在此处下载 EclipseLink 2.3.0:
EclipseLink 2.1.2 的解决方法是使用其他访问类型,或者使用 @XmlTransient 注释相应的字段:
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:
我尝试了你的测试,它为我提供了正确的输出:
它可能是 JAXB2 实现(在你的情况下为 moxy,在我的测试中则为基于本机 JDK1.6 的 JAXB2)。
I tried your test and it gives the correct output for me:
It could be the JAXB2 implementation (moxy in your case vs native JDK1.6 based JAXB2 for my test).