simpleXML xs:choice元素

时间:2012-07-10 19:36:17

标签: xsd simplexml element choice annotate

我有以下内容:

<xs:complexType name="AnswerType">
    <xs:choice minOccurs="1" maxOccurs="1">
        <xs:element name="Checklist" type="ChecklistType" />
        <xs:element name="OptionList" type="OptionListType" />
        <xs:element name="Measurement" type="MeasureType" />
    </xs:choice>
</xs:complexType> 

如何使用SimpleXML注释选择XSD元素?目前我必须将它们全部设置为required = false

@Element(name = "Checklist", required=false)
protected ChecklistType checklist;
@Element(name = "OptionList", required=false)
protected OptionListType optionList;
@Element(name = "Measurement", required=false)
protected MeasureType measurement;

当然有更好的方法。一个人必须要求= true但是如何?

1 个答案:

答案 0 :(得分:1)

我在教程中找不到答案,但我翻到了示例并找到了第二个例子来解决这个问题。在javadocs中查找ElementUnion类。例子是here

@Root
public class Example {

   @ElementUnion({
      @Element(name="text", type=String.class),
      @Element(name="int", type=Integer.class),
      @Element(name="double", type=Double.class)
   })
   private Object value;
}