6

XML と Java オブジェクトの間でマーシャリング/アンマーシャリングを実行するために、Castor から JAXB2 に切り替えています。ポリモーフィック オブジェクトのコレクションを構成しようとすると問題が発生します。

サンプル 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。果物は 3つfruitListあるはずです。オレンジ 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ここで必要なものを実現するには、どのように設定すればよいProjectですか?

どうもありがとう!

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 に答える