简体   繁体   English

在不更改架构的情况下更改类

[英]Changing class without changing a schema

I have a xsd schema which I'm not allowed to change. 我有一个xsd架构,我不允许改变。 It produce generated java classes. 它生成生成的java类。

Assume that classes looks as follows: 假设类看起来如下:

       class Data {
          protected List<Value> value;
          ... 
       }
       class Value {
           ...
        }

Now I need to have my own MyValue ideally to be extended from Value. 现在我需要将自己的MyValue理想地从Value扩展。

class MyValue extends Value {

     Integer myOwnField1;
     Long anotherProperty;

}

And be able to say unmarshaller to use MyValue instead of Value when it parses xml file. 并且能够说unmarshaller在分析xml文件时使用MyValue而不是Value。

Later I would be able to use the fact that MyValue can contain some useful new fields inside, make operations over them, change them, etc. So I want extend the functionality which I have in schema without changing it. 稍后我可以使用MyValue可以在其中包含一些有用的新字段,对它们进行操作,更改它们等等这一事实。所以我想扩展我在架构中的功能而不更改它。

Is that somehow possible to replace Value by MyValue for unmarshaller? 是否有可能通过MyValue替换值为unmarshaller?

Apart from the obvious way to create a Map where I can map object which was generated by unmarshaller with my own fields and properties in MyValue. 除了创建Map的明显方法之外,我可以使用我自己的字段和属性在MyValue中映射由unmarshaller生成的对象。 I'd like to avoid this way. 我想避免这种方式。

Can you load the list using bean properties? 你可以使用bean属性加载列表吗?

public void setValue(List<Value> value) {
    this.value = ...convert to List<MyValue>...
}

You might be interested in unmarshalling by declared type : indeed, if you override your XSD from the root element to every child element you need to override, you could use a second argument in unmarshal method to sepecify your custom mapping. 您可能对通过声明的类型进行解组感兴趣:实际上,如果您将XSD从根元素覆盖到需要覆盖的每个子元素,则可以在unmarshal方法中使用第二个参数来指定自定义映射。

<T> JAXBElement<T> unmarshal(Source source,
                             Class<T> declaredType)
                         throws JAXBException

[...] declaredType - appropriate JAXB mapped class to hold source's xml root element [...] declaredType - 适当的JAXB映射类,用于保存源的xml根元素

(see Unmarshaller javadoc for more detailed info) (有关更多详细信息,请参阅Unmarshaller javadoc

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

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