title: Creating an XML with attributes and dynamic values using JAXB
body: <p>I want to Create an XML using JAXB. 
  It should have attributes in it whose values have to come from a property defined in corresponding class.</p>
  
  <p>The current output is giving a property as a child tag, and not as an attribute in the main tag.</p>
  
  <p>The Structure of my desired XML is as follows.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;company&gt;
      &lt;department name="hr"&gt;
          &lt;branch name="hr-recruitment"&gt;
              &lt;manager&gt; Manasa &lt;/manager&gt;
              &lt;phone&gt;992222222&lt;/phone&gt;
              &lt;salary&gt; 20000 &lt;/salary&gt;
          &lt;/branch&gt;
          &lt;branch name="hr-finance"&gt;
              &lt;manager&gt; Sunder &lt;/manager&gt;
              &lt;phone&gt;993333332&lt;/phone&gt;
              &lt;salary&gt; 50000 &lt;/salary&gt;
          &lt;/branch&gt;
      &lt;/department&gt;
      &lt;department name="transport"&gt;
          &lt;branch name="transport-employee"&gt;
              &lt;manager&gt; Raman &lt;/manager&gt;
              &lt;phone&gt;888888888&lt;/phone&gt;
              &lt;salary&gt; 30000 &lt;/salary&gt;
          &lt;/branch&gt;
          &lt;branch name="transport-goods"&gt;
              &lt;manager&gt; Sheela &lt;/manager&gt;
              &lt;phone&gt;99999999&lt;/phone&gt;
              &lt;salary&gt; 75000 &lt;/salary&gt;
          &lt;/branch&gt;
      &lt;/department&gt;
  &lt;/company&gt; 
  </code></pre>
  
  <p>Here are the 3 classes marked with the JAXB annotations:</p>
  
  <p>1) company.class</p>
  
  <pre><code>@XmlRootElement(name = "tarang")
  public class Company {
  
      private List&lt;Department&gt; listDepartments;
  
      public Company(List&lt;Department&gt; listDepartments) {
          this.listDepartments = listDepartments;
      }
  
      public Company() {
  
      }
  
      @XmlElementRef
      public List&lt;Department&gt; getListDepartments() {
          return listDepartments;
      }
  
      public void setListDepartments(ArrayList&lt;Department&gt; listDepartments) {
          this.listDepartments = listDepartments;
      }
  
  }
  </code></pre>
  
  <p>2) Department.class</p>
  
  <pre><code>@XmlRootElement(name = "department")
  public class Department {
  
      private String strDepartmentName;
  
      private List&lt;Branch&gt; listBranchs;
  
      public Department(String strDepartmentName, List&lt;Branch&gt; listBranchs) {
          this.strDepartmentName = strDepartmentName;
          this.listBranchs = listBranchs;
      }
  
      public Department() {
  
      }
  
      @XmlElement(name = "name")
      public String getStrDepartmentName() {
          return strDepartmentName;
      }
  
      public void setStrDepartmentName(String strDepartmentName) {
          this.strDepartmentName = strDepartmentName;
      }
  
      @XmlElementRef
      public List&lt;Branch&gt; getListBranchs() {
          return listBranchs;
      }
  
      public void setListBranchs(List&lt;Branch&gt; listBranchs) {
          this.listBranchs = listBranchs;
      }
  
  }
  </code></pre>
  
  <p>3) Branch.class</p>
  
  <pre><code>@XmlRootElement(name = "branch")
  public class Branch {
  
      private String strName;
      private String strManagerName;
      private String strPhone;
      private int intSalary;
  
      public Branch(String strName, String strManagerName, String strPhone,
              int intSalary) {
          this.strName = strName;
          this.strManagerName = strManagerName;
          this.strPhone = strPhone;
          this.intSalary = intSalary;
      }
  
      public Branch() {
          // TODO Auto-generated constructor stub
      }
  
      @XmlElement(name = "name")
      public String getStrName() {
          return strName;
      }
  
      public void setStrName(String strName) {
          this.strName = strName;
      }
  
      @XmlElement(name = "manager")
      public String getStrManagerName() {
          return strManagerName;
      }
  
      public void setStrManagerName(String strManagerName) {
          this.strManagerName = strManagerName;
      }
  
      @XmlElement(name = "phone")
      public String getStrPhone() {
          return strPhone;
      }
  
      public void setStrPhone(String strPhone) {
          this.strPhone = strPhone;
      }
  
      @XmlElement(name = "salary")
      public int getIntSalary() {
          return intSalary;
      }
  
      public void setIntSalary(int intSalary) {
          this.intSalary = intSalary;
      }
  
  }
  </code></pre>
  
  <p>My current design contains 3 classes.</p>
  
  <p>1) Company 2) Department 3) Branch</p>
  
  <p>The relations are:</p>
  
  <pre><code>1) class Company -has a- List of Departments.
  2) class Department -has a- name (String).
  3) class Department -has a- List of Branches.
  4) class Branch -has a- name (String)
  5) class Branch -has a- manager (String)
  6) class Branch -has a- phone (String)
  7) class Branch -has a- salary (String)
  </code></pre>
  
  <p>The current Output is:</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
      &lt;tarang&gt;
          &lt;department&gt;
              &lt;branch&gt;
                  &lt;salary&gt;50000&lt;/salary&gt;
                  &lt;manager&gt;Manasa&lt;/manager&gt;
                  &lt;name&gt;hr-recruitment&lt;/name&gt;
                  &lt;phone&gt;999999999&lt;/phone&gt;
              &lt;/branch&gt;
              &lt;branch&gt;
                  &lt;salary&gt;40000&lt;/salary&gt;
                  &lt;manager&gt;Sundar&lt;/manager&gt;
                  &lt;name&gt;hr-finance&lt;/name&gt;
                  &lt;phone&gt;888888888&lt;/phone&gt;
              &lt;/branch&gt;
              &lt;name&gt;hr&lt;/name&gt;
          &lt;/department&gt;
          &lt;department&gt;
              &lt;branch&gt;
                  &lt;salary&gt;30000&lt;/salary&gt;
                  &lt;manager&gt;Raman&lt;/manager&gt;
                  &lt;name&gt;transport-employee&lt;/name&gt;
                  &lt;phone&gt;7777777777&lt;/phone&gt;
              &lt;/branch&gt;
              &lt;branch&gt;
                  &lt;salary&gt;20000&lt;/salary&gt;
                  &lt;manager&gt;Sheela&lt;/manager&gt;
                  &lt;name&gt;transport-goods&lt;/name&gt;
                  &lt;phone&gt;6666666666&lt;/phone&gt;
              &lt;/branch&gt;
              &lt;name&gt;tranport&lt;/name&gt;
          &lt;/department&gt;
      &lt;/tarang&gt;
  </code></pre>
  
  <p>The name of the department, and name of the branch is not getting appended as a attribute.
  Please suggest me the corrections in the classes, where the <code>@xmlattribute</code> annotation has to be applied for me to get the desired output.</p>