Java HTML / XML How to - Write an xml annotation for a self-contained tag with attributes








Question

We would like to know how to write an xml annotation for a self-contained tag with attributes.

Answer

// www . j a v a 2  s.co m
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

public class Main {

  public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Type.class);

    Type type = new Type();

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(type, System.out);
  }

  @XmlRootElement
  public static class Type {

    @XmlAttribute
    protected final String res = "http://www.w3.org/2004/02/java2s#ConceptScheme";

  }
}