gpt4 book ai didi

java - Jaxb 附加包装器围绕基类

转载 作者:行者123 更新时间:2023-12-02 04:18:03 25 4
gpt4 key购买 nike

我有一个子类Child,它派生自基类Parent。我想围绕基类的属性添加一个包装器。

Parent.java

package test;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Parent {

int a;
int b;

String name;

public Parent() {
}

public int getA() {
return a;
}

public void setA(int a) {
this.a = a;
}

public int getB() {
return b;
}

public void setB(int b) {
this.b = b;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

Child.java

package test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Child extends Parent {

private String foo;

public Child() {

}

public String getFoo() {
return foo;
}

public void setFoo(String foo) {
this.foo = foo;
}
}

输出.xml

<child>
<a>1</a>
<b>3</b>
<name>name</name>
<foo>foo</foo>
</child>

但我想要这样的东西:

<child>
<foo>foo</foo>
<parent>
<a>1</a>
<b>3</b>
<name>name</name>
</parent>
</child>

希望有人能帮助我。

最佳答案

您想做的事情是可行的,但可能不值得这么麻烦。您将需要实现 XmlAdapter对于您的 Child 类,

package test;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public final class ChildAdapter extends
XmlAdapter<PrintedType, Child> {

@Override
public Child unmarshal(PrintedType v) throws Exception {
Child ret = new Child();
ret.setFoo(v.foo);
ret.setA(v.parent.getA());
ret.setB(v.parent.getB());
ret.setName(v.parent.getName());
return ret;
}

@Override
public PrintedType marshal(Child v) throws Exception {
PrintedType ret = new PrintedType();
ret.parent = v;
ret.foo = v.getFoo();
return ret;
}
}

以及生成您想要的 XML 表示形式的值类型:

package test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="child")
@XmlAccessorType(XmlAccessType.FIELD)
public class PrintedType {
String foo;
Parent parent;
}

然后,如果您想将 Child 的实例编码(marshal)为根元素(而不是在其他类中拥有 Child 成员字段,然后编码(marshal)该实例)类),您实际上必须首先将 Child 转换为 PrintedType ( https://stackoverflow.com/a/11967459/4854749 )。因此示例测试类可能是

package test;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Demo {
public static void main(String[] args) throws JAXBException {
Child child = new Child();
child.setA(1);
child.setB(3);
child.setName("name");
child.setFoo("foo");

JAXBContext jc = JAXBContext.newInstance(PrintedType.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

PrintedType ret = new PrintedType();
ret.parent = child;
ret.foo = child.getFoo();

marshaller.marshal(ret, System.out);
}
}

您确定这对于您要解决的问题有意义吗?我不知道您的背景,但问问自己以下哪一个听起来更合适:Child is a ParentChild 包含 父级?如果是后者,您可以通过取消继承并使用组合(使 Child contain 一个 Parent 字段)使事情变得更加简单。那么您就不需要适配器类,并且 Child 会完全按照您想要的方式进行编码。

关于java - Jaxb 附加包装器围绕基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33091370/

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