gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:52:44 24 4
gpt4 key购买 nike

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

我有两个类

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com