title: How to get String representation of XmlType?
body: <p>Is it possible to convert a javax.xml.bind.annotation.XmlType to the String representation of the XML?</p>
  
  <p>Example:</p>
  
  <p>The following class Req is from a third party library so I can't override the toString() method.</p>
  
  <pre><code>@javax.xml.bind.annotation.XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.FIELD)
  @javax.xml.bind.annotation.XmlType(name = "req", propOrder = {"myDetails", "customerDetails"})
  public class Req  {
  ...
  }
  </code></pre>
  
  <p>In my application I want to simply get a String representation of the XML so that I can log it to a file:</p>
  
  <pre><code>&lt;Req&gt;
      &lt;MyDetails&gt;
      ...
      &lt;/MyDetails&gt;
      &lt;CustomerDetails&gt;
      ...
      &lt;/CustomerDetails&gt;
  &lt;/Req&gt;
  </code></pre>
  
  <p>When I try to use JAXB and Marshall to convert to XML String:</p>
  
  <pre><code>JAXBContext context = JAXBContext.newInstance(Req.class);
  Marshaller marshaller = context.createMarshaller();
  StringWriter sw = new StringWriter();
  marshaller.marshal(instanceOfReq, sw);
  String xmlString = sw.toString();
  </code></pre>
  
  <p>I get the following exception:</p>
  
  <pre><code>javax.xml.bind.MarshalException
      - with linked exception:
      [com.sun.istack.SAXException2: unable to marshal type "mypackage.Req" as an element because it is missing an @XmlRootElement annotation]
  </code></pre>
  
  <p>I have had a look at the other classes within the third party library and none of them use the @XmlRootElement annotation. Any way around this?</p>