Java HTML / XML How to - Marshal an object via JAXB without any information about it








Question

We would like to know how to marshal an object via JAXB without any information about it.

Answer

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBIntrospector;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.namespace.QName;
/*from ww w.  j a va2 s.com*/
public class Main {

  public static void main(String[] args) throws Exception {
    Object value = "Hello World";

    JAXBContext jc = JAXBContext.newInstance(String.class, Bar.class);
    JAXBIntrospector introspector = jc.createJAXBIntrospector();
    Marshaller marshaller = jc.createMarshaller();
    if (null == introspector.getElementName(value)) {
      JAXBElement jaxbElement = new JAXBElement(new QName("ROOT"),
          Object.class, value);
      marshaller.marshal(jaxbElement, System.out);
    } else {
      marshaller.marshal(value, System.out);
    }
  }

  @XmlRootElement
  public static class Bar {

  }

}