title: Parsing XML File with JAXB based on attribute values
body: <p>I have a Maven and Spring based Java web application.</p>
  
  <p>I have a <strong>config.xml</strong> file in src/main/resources.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;sourceConfigs&gt;
      &lt;areas&gt;
          &lt;area name="Defects"&gt;
              &lt;fileHeaders&gt;ID,Issue Key,Fields
              &lt;/fileHeaders&gt;
          &lt;/area&gt;
          &lt;area name="Organization"&gt;
              &lt;fileHeaders&gt;ID,Org Key,Fields
              &lt;/fileHeaders&gt;
          &lt;/area&gt;
      &lt;/areas&gt;
      &lt;sourceTypes&gt;
          &lt;source name="source1"&gt;
              &lt;adapterObject&gt;source1Adapter&lt;/adapterObject&gt;
              &lt;resultObject&gt;JsonObject&lt;/resultObject&gt;
          &lt;/source&gt;
          &lt;source name="source2"&gt;
              &lt;adapterObject&gt;source2Adapter&lt;/adapterObject&gt;
              &lt;resultObject&gt;sourceObject&lt;/resultObject&gt;
          &lt;/source&gt;
      &lt;/sourceTypes&gt;
  &lt;/sourceConfigs&gt;
  </code></pre>
  
  <p>I want to parse the above XML to Java object based on the attributes.</p>
  
  <p>I have created two classes.</p>
  
  <p><strong>Area.java</strong></p>
  
  <pre><code>@XmlRootElement(name="area")
  public class Area {
          String name;
      String fileHeaders;
  
      @XmlAttribute
      //get Name
      //set Name
  
      @XmlElement
      //get fileHeaders
      //set FileHeaders
  }
  </code></pre>
  
  <p><strong>Source.java</strong></p>
  
  <pre><code>@XmlRootElement(name="source")
  public class Source {
          String name;
      String adapterObject;
      String resultObject;
  
      @XmlAttribute
      //get Name
      //set Name
  
      @XmlElement
      //get adapterObject
      //set adapterObject
  
      @XmlElement
      //get resultObject
      //set resultObject
  }
  </code></pre>
  
  <p>I want to parse the XML based on the attribute value.
   ie; if the attribute value of area is <code>Defects</code>, the parsed object should have the values based on that, else if its <code>Organization</code>, then the values based on that and object type to <strong>Area</strong> object. Similarly for <strong>Source</strong> type also. </p>
  
  <p>How can I do that?</p>
  
  <p>When it was only a simple XML file like following </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;sourceConfig area="Defects"&gt;
      &lt;adapterObject&gt;jAdapter&lt;/adapterObject&gt;
      &lt;resultObject&gt;jsonObject&lt;/resultObject&gt;
  &lt;/sourceConfig&gt;
  </code></pre>
  
  <p>My POJO was based on that and the code I used to parse is</p>
  
  <pre><code>public SourceConfig getConfigObject() throws JAXBException, IOException {
          JAXBContext jaxbContext = JAXBContext.newInstance(SourceConfig.class);
          Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  
          Resource resource=new ClassPathResource("config.xml");
          File file=resource.getFile();
  
          SourceConfig sourceConfig = (SourceConfig) jaxbUnmarshaller.unmarshal(file);
          return sourceConfig;
      }
  </code></pre>
  
  <p>But for this complex I don't know how to parse based on attribute values, and also multiple list of data.</p>
  
  <p>How to parse based on the attribute values?
  I have created two POJOs for parsing different kind. If it's a single class also it's fine.</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>My expected output. </p>
  
  <p>When I pass the value <code>"Defects"</code> when unmarshalling the Area object, The values should be</p>
  
  <pre><code>name= Defects
  fileHeaders=ID,Issue Key,Fields 
  </code></pre>
  
  <p>And If I pass <code>"Organization"</code>, The area object values should be based on that. Similarly when unmarshalling source.</p>
  
  <p>Now I am getting as List of Areas and List of SourceTypes, then I take the corresponding object by checking the value. </p>
  
  <p>May be is there any way to parse only selected one based on attribute value instead of getting list and then checking value and returning the object?</p>