0 Replies Latest reply: Jan 17, 2014 2:39 PM by csh RSS

    JAXB: Map <xs:choice> to enum?

    csh

      Hi,

       

      I am trying to create a reasonable Java representation for this XML schema with JAXB:

       

      XEP-0107: User Mood

       

      Currently I have a new "empty" class like:

       

      @XmlElements({@XmlElement(name = "afraid", type = Afraid.class),

                  @XmlElement(name = "amazed", type = Amazed.class),

                  @XmlElement(name = "angry", type = Amazed.class),

                  @XmlElement(name = "amorous", type = Amazed.class),

      ...

      })

      private Mood mood;

       

      public static final class Afraid extends Mood {

      }

      public static final class Amazed extends Mood {

      }

       

      This works, but I'd rather have an enum for that, so that users can use switch statements and have other comfort like code completion.

      Right now, they have to check for "if (mood instanceof Afraid)"

       

      Something like

      public enum Mood

      {

      AMAZED,

      AFRAID,

      ..

      }

       

      I know I could write a XmlAdapter, but then I still would have to write the whole classes AND the enum, so that's kind of duplicate.

       

      Any suggestions appreciated!