6

我正在从 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 个橙子。但是,我不确定如何配置fruitList. fruitList应该有 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>();
}

水果类

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的 inProject以实现我想要的?

非常感谢!

4

3 回答 3

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>();

}

第 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);
    }

}

了解更多信息:

于 2011-02-03T16:15:51.180 回答
2

在胡闹之后......我想我现在可以工作了: -

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

    @XmlAttribute
    private String      name;

    // this has to be commented out, or else oranges won't show up in fruitList
    // @XmlElement(name = "orange")
    // private List<Orange> oranges = new ArrayList<Orange>();

    @XmlElements({
            @XmlElement(name = "orange", type = Orange.class),
            @XmlElement(name = "apple", type = Apple.class)
    })
    private List<Fruit> fruitList   = new ArrayList<Fruit>();

}
于 2011-02-03T16:22:33.943 回答
0

放 XmlAnyElement 注释:

@XmlAnyElement(lax = true)
private List<Fruit>     fruitList   = new ArrayList<Fruit>();
于 2011-02-03T16:01:20.397 回答