配置多态对象的集合以在JAXB2中工作

lim*_*imc 6 java jaxb jaxb2

我正在从Castor切换到JAXB2,以在XML和Java对象之间执行编组/解组.我在尝试配置多态对象集合时遇到问题.

示例XML

<project name="test project">
    <orange name="fruit orange" orangeKey="100" />
    <apple name="fruit apple" appleKey="200" />
    <orange name="fruit orange again" orangeKey="500" />
</project>
Run Code Online (Sandbox Code Playgroud)

项目类

oranges表工作正常,我看到列表中的2个橘子.但是,我不确定如何配置fruitList.本fruitList应该有3个水果:2个橘子和一个苹果.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Project {

    @XmlAttribute
    private String          name;

    @XmlElement(name = "orange")
    private List<Orange>    oranges     = new ArrayList<Orange>();

    // Not sure how to configure this... help!
    private List<Fruit>     fruitList   = new ArrayList<Fruit>();
}
Run Code Online (Sandbox Code Playgroud)

水果类

水果是一个抽象的类.出于某种原因,将此类定义为抽象似乎会导致很多问题.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Fruit {

    @XmlAttribute
    private String  name;
}
Run Code Online (Sandbox Code Playgroud)

橙类

public class Orange extends Fruit {

    @XmlAttribute
    private String  orangeKey;
}
Run Code Online (Sandbox Code Playgroud)

Apple课程

public class Apple extends Fruit {

    @XmlAttribute
    private String  appleKey;
}
Run Code Online (Sandbox Code Playgroud)

如何配置我的fruitListin Project来实现我想要的?

非常感谢!

bdo*_*han 6

您希望利用@XmlElementRef,这对应于与您的问题对应的替换组的XML架构概念.

第1步 - 使用@XmlElementRef

fruitList属性使用@XmlElementRef注释:

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Project {

    @XmlAttribute
    private String name;

    @XmlElementRef
    private List<Fruit> fruitList = new ArrayList<Fruit>();

}
Run Code Online (Sandbox Code Playgroud)

第2步 - 使用@XmlRootElement注释Apple和Orange

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Apple extends Fruit {

    @XmlAttribute
    private String  appleKey;

}
Run Code Online (Sandbox Code Playgroud)

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Orange extends Fruit {

    @XmlAttribute
    private String  orangeKey;

}
Run Code Online (Sandbox Code Playgroud)

演示代码

以下代码可用于演示解决方案:

import java.io.File;

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Project project = (Project) unmarshaller.unmarshal(new File("input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(project, System.out);
    }

}
Run Code Online (Sandbox Code Playgroud)

欲获得更多信息: