title: How to make JAXB recognize different Classes
body: <p>I've implemented JAXB to convert XML data to Object, here I've used like this.</p>
  
  <p>Assume a new user is Registered. So I've to load the data in its appropriate class to store it in DB. So I used hard coded it as .newInstance(User.Class)</p>
  
  <pre><code>JAXBContext context = JAXBContext.newInstance(User.class);
  </code></pre>
  
  <p>Now I've to do some Employer related functions. 
  For that I need to have it as .newInstance(Employer.class) . It's is possible by copy pasting same jaxb conversion and replacing .newInstance(User.Class) with .newInstance(Employer.Class). But I would like to have the JAXB conversion as Util which can be used for different classes without harcoding the respective Class</p>
  
  <p>I've the XML data like this</p>
  
  <pre><code>&lt;User&gt;
      &lt;username&gt;user&lt;/username&gt;
      &lt;password&gt;password&lt;/password&gt;
  &lt;/User&gt;
  </code></pre>
  
  <p>What I tried to do is, IF I'm going to call the JaxbUtil.java from UserController, I'll store it as </p>
  
  <pre><code>String className = "User.class";
  </code></pre>
  
  <p>If I'm calling it from EmployerComtroller</p>
  
  <pre><code>String className = "Employer.class";
  </code></pre>
  
  <p>and then when calling the method, it'll be like</p>
  
  <pre><code>JaxbUtil.jaxbXMLToObject(xmlData,className);
  </code></pre>
  
  <p>This is my jaxbXMLToObject method in JaxbUtil.java</p>
  
  <pre><code>public static User jaxbXMLToObject(String xmlData,String className) {   //I've passed the xmlString to unmarshaller by StringReader
      User user = null;
      JAXBContext context = JAXBContext.newInstance(className);
      return user; // Ofcourse this is after conversion 
  }
  </code></pre>
  
  <p>Now I hope you guys can understand what I'm trying to say.</p>
  
  <p>1) .newInstance() doesn't accept String</p>
  
  <p>2) Even if I make it recognize (not possible, just assume), I've to change the return type in method declaration.</p>
  
  <p>Is it possible to make this method common for all xml to different Object conversions without creating seperate method for each Object?</p>
  
  <p><strong><em>Solution :</em></strong> </p>
  
  <pre><code>String xmlData; // Assume I've xmlData stored as String
  Class&lt;?&gt; classType = Class.forName("com.package.model.Employer");
  Employer employer = (Employer) JAXBUtil.jaxbXMLToObject(xmlData,classType);    //JaxbUtil is the Util class where the below code is saved
  
  public static Object jaxbXMLToObject(String xmlData, Class&lt;?&gt; classType) throws ClassNotFoundException {
  Object object = null;
      try {
  
          JAXBContext context = JAXBContext.newInstance(classType);
          Unmarshaller unmarshaller = context.createUnmarshaller();
          StringReader reader = new StringReader(xmlData);
          object = (Object) unmarshaller.unmarshal(reader);
      } catch (JAXBException e) {
          e.printStackTrace();
      }
      return object;
  }
  </code></pre>
  
  <p>So I can now call that Util method by changing the class name in each controller.</p>
  
  <p><strong><em>Edit:</em></strong>  Accepted answer is much standardized than my solution posted above. Refer the accepted answer if you've to implement it.</p>