何らかの理由で、要素のすべての値が2回書き込まれます。私のテストケースは非常に単純です。
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.moxy2.1.2です。パッケージフォルダーのjaxb.propertiesファイルには次の行が含まれています。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
ヒントをありがとう。