我希望你能帮助我。
目前我收到xml
格式的两个文件,我想知道如何使用jaxb
将这两个文件关联到同一个类。
Company.xml
<?xml version="1.0" encoding="utf-8"?>
</records>
<record>
<id>4<id>
<companyName>B</companyName>
......
</record>
<record>
<id>7<id>
<companyName>B</companyName>
......
</record>
</records>
Employees.xml
<?xml version="1.0" encoding="utf-8"?>
<records>
<record>
<EmployeeName>AA</EmployeeName>
<CompanyID>2</CompanyID>
</record>
<record>
<EmployeeName>AB</EmployeeName>
<CompanyID>2</CompanyID>
</record>
</records>
我想知道如何生成一个与这些字段id公司相关的课程
public class Companys{
private List<Company> list;
}
public class Company{
private String companyName;
private Integer id;
.........
//I need use JaxB --reference ID company
private List<Employees> list;
}
public class Employee{
private String Name;
private Integer idCompany;
.......................
}
答案 0 :(得分:1)
您可以使用地图。
public class Companies{
private Map<Integer, Company> map = new HashMap<>();
void put(Integer id, Company company) {
map.put(id, company);
}
Company get(Integer id) {
return map.get(id);
}
}
获得员工列表后,您可以遍历这些员工并将其应用到公司地图上。
Company company = companies.get(employee.getIdCompany());
company.addEmployee(employee);
或者,您可以在名为addEmployee
void addEmployee(Employee employee) {
companies.get(employee.getIdCompany()).addEmployee(employee);
}