Question

When I run xjc to generate a Java type representing this XSD snippet:

  <xs:complexType name="fileUploadRequest">
    <xs:choice>
      <xs:element name="path" type="xs:string"/>
      <xs:element name="file" type="xs:base64Binary"/>
    </xs:choice>
  </xs:complexType>

I get a class that's indistinguishable from what it would have been if I'd specified a sequence with optional elements instead.

I want a type with a little bit of intelligence, that'll let me have at most 1 element of my choice at a time. If I invoke the generated setFile method for example, it should make the path null. Is there some plugin I can use for what seems like an obvious requirement of a code generator?

Was it helpful?

Solution

binding.xml

You can use the following external binding file to generate the type of property you are looking for:

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          version="2.1">
    <globalBindings choiceContentProperty="true"/>
</bindings> 

XJC Call

The binding file is referenced using the -b flag.

xjc -b binding.xml schema.xsd

Generated Property

Now the following property will be generated:

@XmlElements({
    @XmlElement(name = "path", type = String.class),
    @XmlElement(name = "file", type = byte[].class)
})
protected Object pathOrFile;

For More Information

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top