2012-09-21 78 views
1

我想使用JAXB創建XML。 它應該有屬性,它的值必須來自相應類中定義的屬性。使用JAXB創建具有屬性和動態值的XML

當前輸出將屬性作爲子標籤給出,而不是作爲主標籤中的屬性。

我想要的XML的結構如下。

<?xml version="1.0" encoding="UTF-8"?> 
<company> 
    <department name="hr"> 
     <branch name="hr-recruitment"> 
      <manager> Manasa </manager> 
      <phone>992222222</phone> 
      <salary> 20000 </salary> 
     </branch> 
     <branch name="hr-finance"> 
      <manager> Sunder </manager> 
      <phone>993333332</phone> 
      <salary> 50000 </salary> 
     </branch> 
    </department> 
    <department name="transport"> 
     <branch name="transport-employee"> 
      <manager> Raman </manager> 
      <phone>888888888</phone> 
      <salary> 30000 </salary> 
     </branch> 
     <branch name="transport-goods"> 
      <manager> Sheela </manager> 
      <phone>99999999</phone> 
      <salary> 75000 </salary> 
     </branch> 
    </department> 
</company> 

下面是標有JAXB註解3類:

1)company.class

@XmlRootElement(name = "tarang") 
public class Company { 

    private List<Department> listDepartments; 

    public Company(List<Department> listDepartments) { 
     this.listDepartments = listDepartments; 
    } 

    public Company() { 

    } 

    @XmlElementRef 
    public List<Department> getListDepartments() { 
     return listDepartments; 
    } 

    public void setListDepartments(ArrayList<Department> listDepartments) { 
     this.listDepartments = listDepartments; 
    } 

} 

2)Department.class

@XmlRootElement(name = "department") 
public class Department { 

    private String strDepartmentName; 

    private List<Branch> listBranchs; 

    public Department(String strDepartmentName, List<Branch> 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<Branch> getListBranchs() { 
     return listBranchs; 
    } 

    public void setListBranchs(List<Branch> listBranchs) { 
     this.listBranchs = listBranchs; 
    } 

} 

3)分支。

@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; 
    } 

} 

我目前的設計包含3個類。

1)公司2)系3)分公​​司

的關係是:

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) 

電流輸出是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <tarang> 
     <department> 
      <branch> 
       <salary>50000</salary> 
       <manager>Manasa</manager> 
       <name>hr-recruitment</name> 
       <phone>999999999</phone> 
      </branch> 
      <branch> 
       <salary>40000</salary> 
       <manager>Sundar</manager> 
       <name>hr-finance</name> 
       <phone>888888888</phone> 
      </branch> 
      <name>hr</name> 
     </department> 
     <department> 
      <branch> 
       <salary>30000</salary> 
       <manager>Raman</manager> 
       <name>transport-employee</name> 
       <phone>7777777777</phone> 
      </branch> 
      <branch> 
       <salary>20000</salary> 
       <manager>Sheela</manager> 
       <name>transport-goods</name> 
       <phone>6666666666</phone> 
      </branch> 
      <name>tranport</name> 
     </department> 
    </tarang> 

部門的名稱,名稱分支不會被附加爲屬性。 請建議我在課程中的更正,其中@xmlattribute註釋必須適用於我以獲得所需的輸出。

@XmlAttribute(name = "name") 
private String strDepartmentName; 

約XmlAttribute註釋更多詳情,請參見here

回答

4

@XmlAttribute應該在你的Department類遵循被添加到字段聲明。

+0

它應該被添加到getter方法,以得到正確的結果..! 否則我們會得到illigal註釋異常.. 感謝您幫助我......! 您的回答是正確的..! – bali208

+3

+1 - @ bali208如果您在類上指定了「@XmlAccessorType(XmlAcccessType.FIELD)」,則可以將註釋放在字段上:http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype- to.html –