JAXB schemagen how to generate selection for two fields?

I have a java class:

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;
}   

I want to get the circuit as follows:

<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>

 

Thus, both the “Elem1” and “Elem2” fields should be in selection.

A solution like this:

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

not suitable for me, because in a java class I need both fields.

But can I do this? Can anybody help?

+4
source share
1 answer

Creating an XML Schema

For the following Java class:

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;
}   

You will get an XML schema as shown below, and there is no way to generate the selection as you would like.

<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>

Indicates a manually created XML schema.

However, you can create an XML schema and then modify it yourself, and then tell JAXB about its use. This is done with package level annotation @XmlSchema.

package-info.java

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

All Articles