我正在尝试使用包装元素来解组XML。
XML看起来:
<?xml version="1.0" encding="utf-8"?>
<Output xmlns="_xxx_" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Companies>
<Company>
...
</Company>
<Company>
...
</Company>
...
</Companies>
<People>
<Person>
...
</Person>
<Person>
...
</Person>
...
</People>
<Relations>
<Relation>
...
</Relation>
<Relation>
...
</Relation>
...
</Relations>
<Info ... />
</Output>
我想在列表中包含补充,人员和关系。我尝试使用此代码,但它不起作用:
Java代码
输出继电器
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Output", namespace = "_xxx_")
@XmlType(propOrder={"companies", "people", "relations", "info"})
public class Output {
@XmlElementWrapper(name = "Companies")
@XmlElement(name = "Company" type = Company.class)
private List<Company> companies;
@XmlElementWrapper(name = "People")
@XmlElement(name = "Person", type = Person.class)
private List<Person> people;
@XmlElementWrapper(name = "Relations")
@XmlElement(name = "Relation", type = Relation.class)
private List<Relation> relations;
@XmlElement(name = "Info")
private Info info;
public Output() {
this.companies = new ArrayList<>();
this.people = new ArrayList<>();
this.relations = new ArrayList<>();
}
public List<Company> getCompanies() {
return companies;
}
public void setCompanies(List<Company> companies) {
this.companies = companies;
}
public List<Person> getPeople() {
return people;
}
public void setPeople(List<Person> people) {
this.people = people;
}
public List<Relation> getRelations() {
return relations;
}
public void setRelations(List<Relation> relations) {
this.relations = relations;
}
public Info getInfo() {
return info;
}
public void setInfo(Info info) {
this.info = info;
}
}
例如 Company 看起来(其他类相似)
@XmlRootElement(name = "Company")
public class Company {
...
}
如果我使用它,它会创建空列表,但是,如果我将代码更改为:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Output", namespace = "_xxx_")
@XmlType(propOrder={"companies", "people", "relations", "info"})
public class Output {
@XmlElement(name = "Companies")
private Companies companies;
@XmlElement(name = "People")
private People people;
....
public Companies getCompanies() {
return companies;
}
public void setCompanies(Companies companies) {
this.companies = companies;
}
...
}
其中公司类看起来:
@XmlRootElement(name = "Companies")
@XmlAccessorType (XmlAccessType.FIELD)
public class Companies {
@XmlElement(name = "Company")
private List<Company> companies;
public List<Company> getCompanies() {
return companies;
}
public void setCompanies(List<Company> companies) {
this.companies = companies;
}
}
它工作正常,但它不是一个很好的解决方案。
有谁能告诉我这里有什么问题?谢谢:))