具有相同名称和不同类型的xmlElements

具有相同名称和不同类型的xmlElements,xml,jaxb,choice,Xml,Jaxb,Choice,) 我有一个xmlelement,它可以来自不同的类型。与类型无关,它具有相同的名称。它可以是一个对象,也可以只是通过URI对现有对象的引用。我认为xmlElements可能是解决方案。编组工作正常,但通过对其进行解编组,它每次都选择最后一个给定的类类型 包含元素的类Flower @XmlRootElement(name = "Flower") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "id"

)

我有一个xmlelement,它可以来自不同的类型。与类型无关,它具有相同的名称。它可以是一个对象,也可以只是通过URI对现有对象的引用。我认为xmlElements可能是解决方案。编组工作正常,但通过对其进行解编组,它每次都选择最后一个给定的类类型

包含元素的类Flower

@XmlRootElement(name = "Flower")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "id", "name", "refName", "description", "created", "updated", "color",
    "seed")
public class Flower extends CommonElements{

private string color;
@XmlElements({
    @XmlElement(name="seed", type=Seed.class),
    @XmlElement(name="seed", type=Reference.class)  
})
public Object seed;

}
类seed是元素可以包含的类型之一

@XmlRootElement(name = "Seed")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "id", "name", "refName", "description", "created", "updated",
    "category", "country"})
public class Seed extends CommonElements{

protected String category = "";
protected String country = "";

}
以及元素可以包含的另一个类

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Reference {
@XmlAttribute(name="href")
protected URI href;
}
在CommonElements中,只有一些通用元素,如id、refName、description等。。 XML可以看起来像

<Flower>
    <id>http://localhost/test/flowers/1</id>
    <refName>redRose</refName>
    <description>classical red rose </description>
    <color>red</color>
    <seed href="http://localhost/test/seeds/1" />
</Flower>

http://localhost/test/flowers/1
红玫瑰
古典红玫瑰
红色
或者

<Flower>
   <id>http://localhost/test/flowers/1</id>
   <refName>redRose</refName>
   <description>classical red rose </description>
   <color>red</color>
   <seed>
      <id>http://localhost/test/seeds/1</id>
      <refName>wildrose</refName>
      <description>Special Seed for beautiful wild roses</description>
      <category>wildrose</category>
      <country>china</country>
  </seed>
</Flower>

http://localhost/test/flowers/1
红玫瑰
古典红玫瑰
红色
http://localhost/test/seeds/1
野玫瑰
美丽野生玫瑰的特殊种子
野玫瑰
中国
我假设类的不同结构足以让jaxb区分对象。 恐怕我必须使用适配器,但我希望有人有另一个好主意

我知道有一个话题和我的相似。但是主题中的类型看起来很相似,因此jaxb无法区分它们。()

谢谢,很抱歉我的英语不好


编辑:是否有一种通过编组添加类型的方法,jaxb确切地知道它是用于解编组的类型

在@XmlElements中不能有相同的名称。代码无法编译。您也可以使用@xmlsee,但这不是正确的方法@XMLSee也适用于子类,但在本例中,它将起作用

此解决方案仅在您知道在

花卉

@XmlRootElement(name=“Flower”)
@XMLSEEALSE({Seed.class,Reference.class})
@XmlAccessorType(XmlAccessType.FIELD)
公共类元素{
公共花卉({}
公用花(字符串id、字符串名称){
super(id,name);
}
@XmlAttribute
私有字符串颜色;
@XmlElement
公共对象种子;
公共字符串getColor(){
返回颜色;
}
公共void setColor(字符串颜色){
这个颜色=颜色;
}
}
2.
红玫瑰
红色
1.
野玫瑰
美国
3.
白玫瑰
白色

mhm。。相同的名称编译和封送完美。但是你的解决方案很有效。就像你说的,XmlSee也意味着其他东西。。所以看起来有点脏。。但只要它有效,对我来说就没问题。:)奇怪。对于我来说,在EclipseJuno和JDK6中,它给出了编译错误.mhm。。现在我得到了[com.sun.istack.internal.SAXException2:“Seed”的实例正在替换“java.lang.Object”,但是“Seed”绑定到了一个匿名类型。]我得到了这个错误,因为我的Seed.class@XmlType(name=”“)中有。。现在,这个错误是有意义的;)
@XmlRootElement(name = "Flower")
@XmlSeeAlso({Seed.class,Reference.class})
@XmlAccessorType(XmlAccessType.FIELD)
public class Flower extends CommonElements {
    public Flower(){}

    public Flower(String id, String name){
        super(id,name);
    }
    @XmlAttribute
    private String color;
    @XmlElement
    public Object seed;

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
}

<Flower>
    <id>2</id>
    <refName>redRose</refName>
    <color>RED</color>
    <seed xsi:type="seed" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <id>1</id>
        <refName>wildrose</refName>
        <category></category>
        <country>USA</country>
    </seed>
</Flower>

<Flower>
    <id>3</id>
    <refName>whiteRose</refName>
    <color>white</color>
    <seed xsi:type="reference" href="www.google.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</Flower>