无论如何在Jaxb中使用超类?

时间:2013-11-07 22:36:04

标签: java xml jaxb

我正在为我的项目建模,我想知道是否有可能减少我在unmarshal过程中重复的代码。

我有两个班级

public class Person {
  private String name;
  private String telephone

  // getter and setter
}


public class Company {
  private String name;
  private String telephone

  // getter and setter
}

我的xml有这个来源。

<file>
  <person>
    <name>Test</name>
    <telephone>190</telephone>
  </person>
  <company>
    <name>Test2</name>
    <telephone>181</telephone>
  </company>
</file>

所以我想知道如何让其他人和Jaxb重新组合超类。

由于

1 个答案:

答案 0 :(得分:1)

您可以使用注释@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文件(将File更改为diff,就像在这种情况下使用FileTest2,原谅可怕的名字),在合成样式中创建类,并进行测试以确保unmarshal可以工作。然后我再次编组以确保我得到了正确的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类完成了这个。我在那里有所有样板文件,我只需要调用我需要的重载方法,以便在要绑定的类中传递marshal或unmarshal,并且所有丑陋的样板文件都远离我的实际代码。

对于想要为具有类似类结构的多个类使用自定义适配器的任何人,您可以在自定义适配器中使用泛型来扩展XmlAdapter并使用注释@XmlJavaTypeAdapter,以便Jaxb将这些通用适配器用于这些类。