Class change without schema change

I have an xsd schema that I cannot change. It creates the generated Java classes.

Suppose the classes are as follows:

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

Now I need my own MyValue to expand perfectly from Value.

 class MyValue extends Value { Integer myOwnField1; Long anotherProperty; } 

And be able to tell unmarshaller to use MyValue instead of Value when it parses an XML file.

Later, I could use the fact that MyValue can contain some useful new fields inside, do operations on them, change them, etc. Therefore, I want to expand the functionality that I have in the circuit without changing it.

How is it possible to replace the value of MyValue for unmarshaller?

Besides the obvious way to create a Map, where I can map an object that was created by unmarshaller to my own fields and properties in MyValue. I would like to avoid this.

+4
source share
2 answers

Can I load a list using bean properties?

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

You might be interested in unmarshalling the declared type : indeed, if you redefine XSD from the root element to each child element, you need an override, you can use the second argument in the unmarshal method to determine your custom mapping.

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

[...] saidType is the corresponding JAXB mapped class to store the original root xml element

(see Unmarshaller javadoc for more details)

0
source

All Articles