Настройка коллекции полиморфных объектов для работы в JAXB2 - PullRequest
5 голосов
/ 03 февраля 2011

Я переключаюсь с 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;
}

Класс Apple

public class Apple extends Fruit {

    @XmlAttribute
    private String  appleKey;
}

Как настроить fruitList в Project чтобы добиться того, что я хочу здесь?

Большое спасибо!

Ответы [ 3 ]

5 голосов
/ 03 февраля 2011

Вы хотите использовать @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 - аннотируйте Apple и Orange с помощью @ XmlRootElement

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

}

Для получения дополнительной информации:

2 голосов
/ 03 февраля 2011

После суеты ... Думаю, теперь все заработало: -

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

}
0 голосов
/ 03 февраля 2011

Поместить аннотацию XmlAnyElement:

@XmlAnyElement(lax = true)
private List<Fruit>     fruitList   = new ArrayList<Fruit>();
...