2

我必须使用 JAXB 创建一个 XML。但它应该有动态标签名称。这就是 POJO 的设计和在其上应用注释方面令人困惑的部分。

POJO 结构应该是什么,类上应该使用哪些注解。

我想要的 XML 的结构如下。

根标签是“公司”,一个公司有很多“部门”,每个部门都有很多动态的“子部门”,每个子部门都有“经理”、“电话”和“工资”。

<company>
    <department>
        <hr>
            <hr-recruitment>
                <manager> Manasa </manager>
                <phone>992222222</phone>
                <salary> 20000 </salary>
            </hr-recruitment>

            <hr-finance>
                <manager> Sunder </manager>
                <phone>993333332</phone>
                <salary> 50000 </salary>
            </hr-finance>
        </hr>
    </department>

    <department>
        <transport>
            <transport-employee>
                <manager> Raman </manager>
                <phone>888888888</phone>
                <salary> 30000 </salary>
            </transport-employee>

            <transport-goods>
                <manager> Sheela </manager>
                <phone>99999999</phone>
                <salary> 75000 </salary>
            </transport-goods>
        </transport>
    </department>

</company> 

请建议我最好的 java 类设计、它们的元素和所需的注释以及结构,以便我可以在我的应用程序中以更好的方式使用 Jaxb。

我目前的设计包含 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)

(我对上述设计的效率和有效性有疑问,请证明其正确性)。

以下是标有 JAXB 注释的 3 个类:

1) 公司.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) 部门.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) 分支.class

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

}

当前输出为:

<?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>

部门名称和分支名称既不会作为单独的标记,也不会作为属性附加。请通知注释标记中的更正以获得我想要的输出。

4

1 回答 1

3

如果事先知道所有标签名称,那么您可以使用 、 和 的组合@XmlElementRef@XmlElementDecl映射JAXBElement您的用例。

但是,从长远来看,您可能会更喜欢更直接地表示您的数据。

<?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> 
于 2012-09-17T11:25:56.357 回答