是否有任何类似@XmlElementWrapper的注释,但没有用于集合的注释

时间:2019-07-18 05:44:38

标签: java jaxb xml-binding

我遇到以下问题:

我有一个具有以下结构的.xml文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<first-tag>
    <second-tag>
        <one-out-of-three-tags>
            <some-element>1</some-element>
            <a-second-element>2</a-second-element>
        </one-out-of-three-tags>
    </second-tag>
</first-tag>

我已经设法为one-out-of-three标签建立了三种实现的接口,但是如果没有包装类,就无法添加second-tag

我正在寻找类似于@XmlElementWrapper的注释,该注释接受单个元素而不是集合,以便避免使用几乎毫无用处的包装器类。

下面的Java类:

@XmlRootElement(name = "first-tag")
@XmlAccessorType(FIELD)
public class FirstClass {

//this is what I am trying to avoid and replace with 
//@SomeAnnotation
//InterfaceOfThree oneOutOfThree;

  @XmlElement(name = "second-tag")
  SecondClass secondClass;
}
public class SecondClass {

  @XmlElements({
      @XmlElement(name = "first-of-three", type = FirstOfThree.class),
      @XmlElement(name = "second-of-three", type = SecondOfThree.class),
      @XmlElement(name = "third-of-three", type = ThirdOfThree.class)
  })
  InterfaceOfThree oneOutOfThree;
}
@XmlSeeAlso({FirstOfThree.class, SecondOfThree.class, ThirdOfThree.class})
public abstract class InterfaceOfThree {
}
@XmlType(name = "first-of-three")
@XmlRootElement(name = "first-of-three")
@XmlAccessorType(FIELD)
public class FirstOfThree extends InterfaceOfThree {

  @XmlElement
  String someElement;

  @XmlElement
  String aSecondElement;

}
@XmlType(name = "second-of-three")
@XmlRootElement(name = "second-of-three")
@XmlAccessorType(FIELD)
public class SecondOfThree extends InterfaceOfThree {

  @XmlElement
  String someElement;

  @XmlElement
  String aSecondElement;

}
@XmlType(name = "third-of-three")
@XmlRootElement(name = "third-of-three")
@XmlAccessorType(FIELD)
public class ThirdOfThree extends InterfaceOfThree {

  @XmlElement
  String someElement;

  @XmlElement
  String aSecondElement;

}

0 个答案:

没有答案