prev | table of contents | next |
The schema element xsd:choice
lets you define a type for
an XML element which has a content of exactly one element from a given
set of alternatives.
<xsd:complexType name="CommType"> <xsd:choice> <xsd:element name="SMS" type="xsd:string"/> <xsd:element name="MMS" type="xsd:string"/> <xsd:element name="Email" type="xsd:string"/> </xsd:choice> </xsd:complexType>Although only one out of the three elements will actually be present, the generated Java class provides instance variables and getters and setters for all alternatives.
public class CommType { protected String sms; protected String mms; protected String email; public String getSMS() { return sms; } public void setSMS(String value) { this.sms = value; } // ...(more getters and setters) }Although a handful of unused references isn't all that expensive, a Java class like this just doesn't have a nice feeling about it. Also, using such a class may easily lead to errors that are hard to track down. There is, for instance, nothing in the generated code that will keep you from calling more than one setter.
Object oriented languages have no unions because a set of alternative structures is meant to be implemented by a set of subclasses. This topic is discussed in the section Defining Subtypes.
prev | table of contents | next |