您可以使用注释 @XmlSeeAlso 来完成此操作。
@XmlSeeAlso({Person.class,Company.class})
public class Contact {
private String name;
private String telephone
//getters and setters
}
public class Person extends Contact {
}
public class Company extends Contact{
}
那么你可以
JAXBContext.newInstance(Contact.class)
它将允许两个类由相同的代码处理
好的,下面是我所做的。我拿了你的 xml 文件(在这种情况下将文件更改为 FileTest2,请原谅可怕的名称),以组合样式创建类,并进行测试以确保解组可以正常工作。然后我再次封送它以确保我得到正确的 xml 结构,请参见下面的代码。
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
private String name;
private String telephone;
public Person() {
super();
}
public Person(String name, String telephone){
this();
setName(name);
setTelephone(telephone);
}
public String getName(){
return name;
}
public String getTelephone(){
return telephone;
}
public void setName(String name){
this.name=name;
}
public void setTelephone(String telephone){
this.telephone=telephone;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Company {
private String name;
private String telephone;
public Company() {
super();
}
public Company(String name, String telephone){
this();
setName(name);
setTelephone(telephone);
}
public String getName(){
return name;
}
public String getTelephone(){
return telephone;
}
public void setName(String name){
this.name=name;
}
public void setTelephone(String telephone){
this.telephone=telephone;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class FileTest2 {
private Person person;
private Company company;
public FileTest2() {
super();
}
public FileTest2(Person person, Company company){
this();
setPerson(person);
setCompany(company);
}
public Person getPerson() {
return this.person;
}
public void setPerson(Person person) {
this.person = person;
}
public Company getCompany() {
return this.company;
}
public void setCompany(Company company) {
this.company = company;
}
}
@Test
public void testUnMarshal() throws Exception {
File xmlFile = null;
try {
xmlFile = getXmlFile();
} catch (Exception e) {
}
if (xmlFile == null) {
fail("file not found");
}
FileTest2 filetest = (FileTest2) JaxbUtil.unmarshal(
xmlFile, filetest.class,
FileTest2.class, Person.class, Company.class);
if (filetest==null){
fail("unmarshal failed");
}
assertEquals("Company name was not correctly handled","Test2",filetest.getCompany().getName());
assertEquals("Company telephone was not correclty handled","181", filetest.getCompany().getTelephone());
assertEquals("Person name was not correctly handled","Test",filetest.getPerson().getName());
assertEquals("Person telephone was not correclty handled","190", filetest.getPerson().getTelephone());
}
@Test
public void testMarshal() throws Exception {
Person person = new Person("bob", "23");
Company company = new Company("accountTemps", "99");
FileTest2 filetest = new FileTest2(person, company);
FileWriter fileWriter = new FileWriter("marshaledFileResults.xml");
JaxbUtil.marshal(filetest, fileWriter, true, filetest.class,
FileTest2.class, Person.class, Company.class);
}
testMarshal 产生了以下内容
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<fileTest2>
<person>
<name>bob</name>
<telephone>23</telephone>
</person>
<company>
<name>accountTemps</name>
<telephone>99</telephone>
</company>
</fileTest2>
所以,我想你想知道的是如何避免重复编组和解组所需的所有样板。我通过我的 JaxbUtil 类实现了这一点。我有所有的样板文件,我只需要调用我需要的重载方法来编组或解组传入要绑定的类,并且所有丑陋的样板文件都远离我的实际代码。
此外,对于想要为具有相似类结构的多个类使用自定义适配器的任何人,您可以在扩展 XmlAdapter 的自定义适配器中使用泛型并使用注释 @XmlJavaTypeAdapter,以便 Jaxb 将您的泛型适配器用于这些类。