title: Programmatically add or set JAXB XmlRootElement
body: <p>I want to avoid adding an annotation @XMLRootElement to every POJO that I will used to unmarshall a xml. Is there a way to set or add programmatically XmlRootElement to a class?</p>
  
  <pre><code> //@XmlRootElement here
      public class Person{
      private String name;
      private String address;
      //other fields
      //getters and setters here
  </code></pre>
  
  <p>If not possible, how can I unmarshall a xml without XmlRootElement in POJO?</p>
  
  <pre><code>public static &lt;T&gt; T unmarshal(Class clazz, String xml) {
      try {
          JAXBContext ctx = JAXBContext.newInstance(clazz);
          Unmarshaller u = ctx.createUnmarshaller();
          return (T) u.unmarshal(new StringReader(xml));
      } catch (JAXBException e) {
          throw new RuntimeException("Error interpreting XML response", e);
      }
  }
  </code></pre>
  
  <p>Exception thrown without @XmlRootElement:</p>
  
  <pre><code>Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"report"). Expected elements are (none)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:556)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:199)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:194)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:71)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:962)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:399)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:380)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:101)
  at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
  at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
  at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
  at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
  at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
  at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
  at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
  at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
  at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
  at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:195)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:168)
  at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
  at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
  </code></pre>
  
  <p><strong>EDITED:</strong></p>
  
  <p>Blaise answer was actually working.</p>
  
  <pre><code>StreamSource source = new StreamSource(new StringReqder(xml));
  return (T) u.unmarshal(source, clazz).getValue();
  </code></pre>
  
  <p>but when I'm trying to used it to return a List</p>
  
  <pre><code>//this works when I specify @XmlRootElement
  private static &lt;T&gt; List&lt;T&gt; unmarshallCollection(Class&lt;T&gt; clazz, Source source)
              throws JAXBException {
          JAXBContext ctx = JAXBContext.newInstance(WrapperCollection.class, clazz);
          Unmarshaller u = ctx.createUnmarshaller();
          WrapperCollection&lt;T&gt; collection = u.unmarshal(source, WrapperCollection.class).getValue();
          return collection.getItems();
      }
  </code></pre>
  
  <p>and it throws an <em>exception cannot org.apache.xerces.dom.ElementNSImpl cannot be cast to ..</em>
  What did I do wrong?</p>