Question

What is the best way to represent in java a "choice" namespace tag? ie.

<xs:complexType name="MyType">
  <xs:sequence>
    <!-- common elements here -->
    <xs:element type="xs:string" name="name" ... />
  </xs:sequence>
  <xs:choice>
    <xs:element name="stringValue" type="xs:string"></xs:element>
    <xs:element name="intValue" type="xs:int"></xs:element>
  </xs:choice>
</xs:complexType>

How do I model this in Java? I was thinking about something like:

public class MyType
  String name;

  String stringValue;
  int intValue;
...

but this is sure not the best way, or I am wrong? Plus, if I want to expose services with Axis2 which use that type, do I need to implement some custom message receiver?

Was it helpful?

Solution

We usually translate the xsd to Java objects with jaxb or some other binding mechanism (of which you have several in axis2). Those generate objects exactly like you have shown: that is, all attributes in the choices are there and you don't see anything that indicates that only one of them can be present. If you are translating the stuff back into xml, this would be only noticed if you switch on validation. There is no problem with choices if you aware that you shouldn't set both variants simultaneously in Java - the result can sometimes even be that none of them makes it into the generated XML.

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