Java HTML / XML How to - Marshal enum value








Question

We would like to know how to marshal enum value.

Answer

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlRootElement;
//from w  ww.  j a v a2  s.co m
public class Main {

  public static void main(String[] args) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(Card.class);
    context.createMarshaller().marshal(Card.CLUBS, System.out);
  }

}

@XmlRootElement
@XmlEnum(String.class)
enum Card {
  CLUBS, DIAMONDS, HEARTS, SPADES
}

The code above generates the following result.