title: JAXBContext: How to dynamically load objects based on particular attribute?
body: <p>I have three versions of xml with same root element name and child element will vary on each version.</p>
  <blockquote>
  <p>Note: In my application, we are using large xml. So I simplified the
  examples below but the requirement is exactly same.</p>
  </blockquote>
  <p>Sample XMLs below:</p>
  <pre><code>&lt;Tutorial version=&quot;1.0&quot;&gt;
      &lt;FieldA&gt;aaa&lt;/FieldA&gt;
      &lt;FieldB&gt;bbb&lt;/FieldB&gt;
      &lt;FieldC&gt;ccc&lt;/FieldC&gt;
  &lt;/Tutorial&gt;
  
  &lt;Tutorial version=&quot;2.0&quot;&gt;
      &lt;FieldD&gt;ddd&lt;/FieldD&gt;
      &lt;FieldE&gt;eee&lt;/FieldE&gt;
      &lt;FieldF&gt;fff&lt;/FieldF&gt;
  &lt;/Tutorial&gt;
  
  &lt;Tutorial version=&quot;3.0&quot;&gt;
      &lt;FieldG&gt;ggg&lt;/FieldG&gt;
      &lt;FieldH&gt;hhh&lt;/FieldH&gt;
      &lt;FieldI&gt;iii&lt;/FieldI&gt;
  &lt;/Tutorial&gt;
  </code></pre>
  <p>I have three different data structure for each version like below:</p>
  <pre><code>@XmlAccessorType(XmlAccessType.Field)
  @XmlRootElement(name=&quot;Tutorial&quot;)
  public class Tutorialv1
  {
  @XmlElement(name=&quot;FieldA&quot;)
  String fieldA;
  @XmlElement(name=&quot;FieldB&quot;)
  String fieldB;
  @XmlElement(name=&quot;FieldC&quot;)
  String fieldC;
  }
  
  @XmlAccessorType(XmlAccessType.Field)
  @XmlRootElement(name=&quot;Tutorial&quot;)
  public class Tutorialv2
  {
  @XmlElement(name=&quot;FieldD&quot;)
  String fieldD;
  @XmlElement(name=&quot;FieldE&quot;)
  String fieldE;
  @XmlElement(name=&quot;FieldF&quot;)
  String fieldF;
  }
  
  @XmlAccessorType(XmlAccessType.Field)
  @XmlRootElement(name=&quot;Tutorial&quot;)
  public class Tutorialv3
  {
  @XmlElement(name=&quot;FieldG&quot;)
  String fieldG;
  @XmlElement(name=&quot;FieldH&quot;)
  String fieldH;
  @XmlElement(name=&quot;FieldI&quot;)
  String fieldI;
  }
  </code></pre>
  <p>Based on the version attribute of Tutorial element, I have to populate respective data structure. Now I am using JAXB to parse the xml into objects but prior to that I am checking the version using regex like below:</p>
  <pre><code>private final Pattern VERSION_REGEX=Pattern.compile(&quot;&lt;Tutorial version=\&quot;([^\&quot;]+|)&quot;);
  String version = null;
  Matcher versionMatcher=VERSION_REGEX.matcher(xmlSource);
  if(versionMatcher.find()) {
      version = versionMatcher.group(1);
  }
  
  Reader reader = new StringReader(xmlSource);
  XMLStreamReader xmlReader = xmlFactory.createXMLStreamReader(reader);
  
  switch (version) {
      case &quot;1.0&quot;:
          Unmarshaller unmarshaller = JAXBContext.newInstance(Tutorialv1.class).createUnmarshaller();
          JAXBElement&lt;Tutorialv1&gt; tutorial = unmarshaller.unmarshal(xmlReader, Tutorialv1.class);
  
          return otherBusinessLogicGoesHere(tutorial);
      case &quot;2.0&quot;:
          Unmarshaller unmarshaller = JAXBContext.newInstance(Tutorialv2.class).createUnmarshaller();
          JAXBElement&lt;Tutorialv2&gt; tutorial = unmarshaller.unmarshal(xmlReader, Tutorialv2.class);
          
          return otherBusinessLogicGoesHere(tutorial);
      case &quot;3.0&quot;:
      default:
          Unmarshaller unmarshaller = JAXBContext.newInstance(Tutorialv3.class).createUnmarshaller();
          JAXBElement&lt;Tutorialv3&gt; tutorial = unmarshaller.unmarshal(xmlReader, Tutorialv3.class);
          
          return otherBusinessLogicGoesHere(tutorial);
  }
  </code></pre>
  <p>As of now, I used above solution but I feel that it is not good idea to check version using regex. Is there any other solution available to dynamically load objects based on version attribute using JAXB parser?</p>