我在尝试解码对象时遇到了继承问题。这是我的类(class)
A
@XmlRootElement(name="A")
public abstract class A{
}
B
@XmlRootElement(name="B")
public class B extends A{
String bField;
@XmlAttribute(name="b")
public String getBField(){
return bField;
}
public void setBField(String value){
this.bField = value;
}
}
C
@XmlRootElement(name="C")
public class C extends A{
String cField;
@XmlAttribute(name="c")
public String getCField(){
return cField;
}
public void setCField(String value){
this.cField = value;
}
}
容器
@XmlRootElement(name="container")
public class Container{
ArrayList<B> listB;
ArrayList<C> listC;
public ArrayList<B> getListB(){
return listB;
}
@XmlElementWrapper(name="list-B")
@XmlElement(name="b")
public ArrayList<B> getListB(){
return listB;
}
@XmlElementWrapper(name="list-C")
@XmlElement(name="c")
public ArrayList<C> getListC(){
return listC;
}
public ArrayList<C> getListC(){
return listC;
}
}
然后输入XML文件
<container>
<list-B>
<b b="BFied"/>
</list-B>
<list-C>
<c c="CField"/>
</list-C>
</container>
我使用了与 Spring OXM 集成的 EclipseLink JAXB。当我将 xml 文件解码到容器实例时,所有内容都会重复。在列表 B 中,我有 2 个重复的 B 实例(与列表 C 相同)。
请告诉我我哪里做错了?谢谢!
最佳答案
注意:我是EclipseLink JAXB (MOXy) JAXB (JSR-222) 的领导者和成员专家组。
我无法重现您所看到的问题。我使用的是 EclipseLink 2.4.0,可以从以下位置获取:
以下是我根据您的问题编写的完整代码:
A
package forum11642669;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="A")
public abstract class A{
}
B
package forum11642669;
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "B")
public class B extends A {
String bField;
@XmlAttribute(name = "b")
public String getBField() {
return bField;
}
public void setBField(String value) {
this.bField = value;
}
}
C
package forum11642669;
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "C")
public class C extends A {
String cField;
@XmlAttribute(name = "c")
public String getCField() {
return cField;
}
public void setCField(String value) {
this.cField = value;
}
}
容器
您问题中的 Container
类的版本无法编译,因此我对其进行了如下修改:
package forum11642669;
import java.util.ArrayList;
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "container")
public class Container {
ArrayList<B> listB;
ArrayList<C> listC;
@XmlElementWrapper(name = "list-B")
@XmlElement(name = "b")
public ArrayList<B> getListB() {
return listB;
}
public void setListB(ArrayList<B> listB) {
this.listB = listB;
}
@XmlElementWrapper(name = "list-C")
@XmlElement(name = "c")
public ArrayList<C> getListC() {
return listC;
}
public void setListC(ArrayList<C> listC) {
this.listC = listC;
}
}
jaxb.properties
要将 MOXy 指定为您的 JAXB 提供程序,您需要在与您的域模型相同的包中包含一个名为 jaxb.properties
的文件,其中包含以下条目(请参阅: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html )
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示
package forum11642669;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Container.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11642669/input.xml");
Container container = (Container) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(container, System.out);
}
}
输入.xml/输出
<?xml version="1.0" encoding="UTF-8"?>
<container>
<list-B>
<b b="BFied"/>
</list-B>
<list-C>
<c c="CField"/>
</list-C>
</container>
关于java - EclipseLink 和 Spring OXM 继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11642669/