我从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>

项目类
oranges列表工作正常,我在列表中看到2个橙色。但是,我不确定如何配置fruitListfruitList应该有3个水果:2个橙子和1个苹果。
@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>();
}

水果类

水果是抽象类。由于某种原因,将该类定义为抽象类似乎会引起很多问题。
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Fruit {

    @XmlAttribute
    private String  name;
}

橙色类
public class Orange extends Fruit {

    @XmlAttribute
    private String  orangeKey;
}

苹果类
public class Apple extends Fruit {

    @XmlAttribute
    private String  appleKey;
}

如何在fruitList中配置我的Project以实现我在这里想要的?

非常感谢!

最佳答案

您要利用@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>();

}

步骤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;

}


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;

}

演示代码

以下代码可用于演示解决方案:
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);
    }

}

有关更多信息:
  • http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-substitution.html
  • 07-28 13:00