java - 无论如何可以在 Jaxb 中使用父类(super class)吗?

标签 java xml jaxb

我正在对我的项目进行建模,我想知道是否可以减少解码过程中的代码重复。

我有两个类

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>

所以我想知道我应该做什么才能让父类(super class)被其他人和 Jaxb 识别。

谢谢

最佳答案

您可以使用注释 @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,请原谅可怕的名称),以组合样式创建类,并进行测试以确保解码可以工作。然后我再次整理它以确保我得到了正确的 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 类完成此任务。我在那里有所有的样板文件,我只需要调用我需要的重载方法来编码或解码传递要绑定(bind)的类,并且所有丑陋的样板文件都远离我的实际代码。

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

关于java - 无论如何可以在 Jaxb 中使用父类(super class)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19848132/

相关文章:

java - 写入 OutputStream 时测量中间吞吐量 [或] 为什么 write() 会阻塞,而 read() 不会?

java - 使用流更改 ArrayList 中对象字段的值,使代码更高效

java - 您如何向用户表示 XML 文档中的位置?

java - 如何让JAXB识别@XmlElement(默认='something')注释参数?

java - 在两个不同的 JAVA 包下使用相同的对象?

java - 如何在我的 Java 游戏中正确实现 JScrollPane GUI?

Java .jar 未在 unix 中运行

java - Jaxb 强制继承类的属性首先被解码

Android XML 对象序列化

java - Eclipse Java - Jaxb2 插件和 WSDL