views:

259

answers:

1

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?

+1  A: 

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.

hstoerr
This way the server side seems to work, but there is a problem: the default axis2 serializer put also the null elements setting xsi:nil="true" in the response (ie. <stringValue xsi:nil="true"/><intValue>4<..). Obviously this make the client parser unhappy, because it doesn't expect another element!
cheng81
Sounds to me like a bug you should report. This is certainly not right and I've not seen such behaviour in what we use.
hstoerr