简体   繁体   English

Java JAXB如何将XmlElements重新定义为现有变量

[英]Java JAXB how to redefine XmlElements to existing variable

When unmarshalling using jaxb, I have in class A something along: 当使用jaxb进行解组时,我在A类中有一些东西:

public class A {
    @XmlElements( { //
    @XmlElement(name = "g", type = A.class),
        @XmlElement(name = "x", type = X.class), 
        @XmlElement(name = "y", type = Y.class),        
    })
    List<XXX> children;
}

That is, I have a list, children, consisting of X:s and Y:s 也就是说,我有一个列表,孩子,由X:s和Y:s组成

Now for my question: I would like to subclass A, and I would like to redefine the 'XmlElements' list and bind it to the same variable, 'children', like: 现在我的问题是:我想继承A,我想重新定义'XmlElements'列表并将其绑定到同一个变量'children',如:

public class B extends A {
    @XmlElements( { //
    @XmlElement(name = "g", type = B.class),
        @XmlElement(name = "x", type = X.class), 
        @XmlElement(name = "y", type = Y.class), 
        @XmlElement(name = "z", type = Z.class),        
    })
    List<XXX> children;
}

The issues with the above are twofold: 以上问题有两个方面:

  1. I create a new variable children, I would like to refer to the variable in class A. 我创建了一个新的变量children,我想引用A类中的变量。

  2. I would like to avoid respecifying the 'x' and 'y' since they are already specified in 'A'. 我想避免重新指定'x'和'y',因为它们已在'A'中指定。

Is there some good pattern to achieve this? 是否有一些良好的模式来实现这一目标?

Or some pointers/articles or other info for how to build something like this? 或者一些关于如何构建这样的东西的指针/文章或其他信息?

There's no avoiding the re-declaration of the annotation, but you can move the annotation from the field on to the getter method, as long as you use the @XmlAccessorType annotation to tell JAXB to look for public getter methods rather than fields. 没有避免重复声明注释,但是您可以将注释从字段移动到getter方法,只要您使用@XmlAccessorType注释告诉JAXB查找公共getter方法而不是字段。

So you could override getChildren() in class B wit the new annotation set: 因此,您可以使用新的注释集覆盖B类中的getChildren()

@XmlAccessorType(PROPERTY)
public class A {
    private List<XXX> children;

    @XmlElements( { //
    @XmlElement(name = "g", type = A.class),
        @XmlElement(name = "x", type = X.class), 
        @XmlElement(name = "y", type = Y.class),        
    })
    public List<XXX> getChildren() {
       return children;
    }

    public void setChildren(List<XXX> children) {
       this.children = children;
    }
}

@XmlAccessorType(PROPERTY)
public class B extends A {
    @XmlElements( { //
    @XmlElement(name = "g", type = B.class),
        @XmlElement(name = "x", type = X.class), 
        @XmlElement(name = "y", type = Y.class), 
        @XmlElement(name = "z", type = Z.class),        
    })
    public List<XXX> getChildren() {
       return super.getChildren();
    }
}

What I'm not sure about is how JAXB will handle the overriden getChildren() method. 我不确定的是JAXB将如何处理重写的getChildren()方法。 Hopefully it'll take the annotations from B , but it's possible it'll get confused. 希望它能从B获取注释,但它可能会让人感到困惑。

Try it and see. 试试看吧。

JAXB will actually handle the overriden method, when PROPERTY @XmlAccessorType is used in parent, but as a result in output xml data JAXB probably will also generate additionally in the root tag attributes such as: xsi:type="B" and xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" , because we defined a new type B inherited from A. So xml produced from class A won't be identical to that produced from class B because of those two attributes correctly added by JAXB to inform that we defined additional type B. 当在父级中使用PROPERTY @XmlAccessorType时,JAXB实际上将处理overriden方法,但是在输出xml数据中,JAXB可能还会在根标记属性中另外生成,例如: xsi:type =“B”和xmlns:xsi =“http://www.w3.org/2001/XMLSchema-instance” ,因为我们定义了一个从A继承的新类型B.因此,从类A生成的xml与从类B生成的类型不同JAXB正确添加了两个属性,以通知我们定义了其他类型B.

I would like to see a way to avoid this xsi and xmlns information in output xml. 我想看到一种方法来避免输出xml中的这个xsixmlns信息。 This behavior of JAXB is perfectly fine but for me it would be great to not inform receiver in my xml output that I've made class extending original class that I took possibly from client xsd schema. JAXB的这种行为非常好,但对我来说,在我的xml输出中不通知接收器,我已经使类扩展了我可能从客户端xsd模式中获取的原始类,这将是很好的。

To produce xml from derived subclass but w/o the xsi and xmlns attributes added by JAXB I used temporary trick which I am trying to exchange for something nicer: I did override of method in child, in parent overriden method I had to use @XmlElement but I did not added inherited class type to the JAXB.newinstance call. 要从派生子类生成xml但是没有由JAXB添加的xsixmlns属性我使用临时技巧我试图更换更好的东西:我在子窗体中覆盖了方法,在父覆盖方法中我不得不使用@XmlElement但是我没有将继承的类类型添加到JAXB.newinstance调用中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM