JAXB schemagen如何生成两个字段的选择?

时间:2014-10-31 17:11:39

标签: jaxb schemagen

我有java类:

public class ActivityAddress {

    @XmlElement(name = "Elem1", required = false)
    private String elem1;

    @XmlElement(name = "Elem2", required = false)
    private String elem2;

    @XmlElement(name = "PostIndex", required = true)
    private String postIndex;
}   

我想得到这样的架构:

<xs:complexType name="ActivityAddress">
<xs:sequence>
  <xs:choice minOccurs="0">
    <xs:element name="Elem1" type="xs:string"/>
    <xs:element name="Elem2" type="xs:string"/>
  </xs:choice>
  <xs:element name="PostIndex" type="xs:string"/>
</xs:sequence>

   

因此必须选择两个字段“Elem1”和“Elem2”。

这样的决定:

@XmlElements({
    @XmlElement(name = "Elem1", type = String.class, required = false),
    @XmlElement(name = "Elem2", type = String.class, required = false)
})
private String elem;

不适合我,因为在java类中我需要两个字段。

Н我能做到吗?有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

生成XML架构

对于以下Java类:

public class ActivityAddress {

    @XmlElement(name = "Elem1", required = false)
    private String elem1;

    @XmlElement(name = "Elem2", required = false)
    private String elem2;

    @XmlElement(name = "PostIndex", required = true)
    private String postIndex;
}   

您将获得如下所示的XML Schema,无法按照您希望的方式生成选择。

<xs:sequence>
    <xs:element name="Elem1" type="xs:string" mincOccurs="0"/>
    <xs:element name="Elem2" type="xs:string" minOccurs="0"/> 
    <xs:element name="PostIndex" type="xs:string"/>
</xs:sequence>

指向手工制作的XML架构

然而,您可以生成XML Schema,然后自己修改它,然后告诉JAXB使用它。这是通过包级别@XmlSchema注释完成的。

<强> package-info.java

@XmlSchema(location="http://www.example.com/foo/mySchema.xsd")
package com.example.foo;