Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基类周围的Jaxb附加包装器_Java_Xml_Jaxb - Fatal编程技术网

Java 基类周围的Jaxb附加包装器

Java 基类周围的Jaxb附加包装器,java,xml,jaxb,Java,Xml,Jaxb,我有一个子类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()

我有一个子类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;
    }
}
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;
    }
}
Child.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;
    }
}
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;
    }
}
output.xml

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

1.
3.
名称
福
但我想要这样的东西:

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

福
1.
3.
名称

我希望有人能帮我。

你想做的是可行的,但可能不值得麻烦。您需要为您的
子类
实现

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;
    }
}
然后,如果要封送
Child
的实例作为根元素(而不是在其他类中有
Child
成员字段并封送该类),实际上必须首先将
Child
转换为
PrintedType
)。因此,一个示例测试类可能是

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
包含
Parent
字段)使事情变得更简单。这样,您就不需要适配器类,
Child
将以您希望的方式进行封送。

为Child提出“包含”重构是完全足够的。如果所需的XML结构有任何意义,那么子对象由父对象和foo属性组成+1(请注意@Clemens:在Child中使用委托,以便轻松访问其父组件。)