title: How to create a JAXB Marshaller that takes a Generic object
body: <p>Essentially what I'm trying to do is create a marshaller that can take any class object I give it for example a car object or a person object and it must return a XML string.</p>
  
  <p>Here is what I've got so far:</p>
  
  <pre><code> public &lt;T&gt; String CreateXML(T objToSerialize)
      {
          String xml = "";
          try 
          {           
  
          JAXBContext context = JAXBContext.newInstance(objToSerialize.getClass());
          Marshaller marshaler = context.createMarshaller(); 
          StringWriter writer = new StringWriter();
          marshaler.marshal(objToSerialize.getClass(),writer);
          xml = writer.toString();
              System.out.println(xml);
              return xml;
          } catch (Exception e) {
              System.out.println(e.getMessage());
          }
          return xml;
      }
  </code></pre>
  
  <p>It gives me the following error:</p>
  
  <blockquote>
    <p>WARNING: An illegal reflective access operation has occurred</p>
  </blockquote>