...">

Choosing xjc and XSD

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

<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 is indistinguishable from what would happen if I specified a sequence with optional elements.

I need a type with a little intelligence that will allow me to have no more than 1 element of my choice at a time. If I call the generated setFile method, for example, it should make the path null. Is there any plugin that I can use for what seems like an obvious requirement of a code generator?

+4
source share
1 answer

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 challenge

The linked file is referenced using the -b flag.

 xjc -b binding.xml schema.xsd 

Generated property

Now the following property will be created:

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

Additional Information

+5
source

Source: https://habr.com/ru/post/1500581/


All Articles