Question

I am falling foul of the limitaion of jaxb's XMLAdapters when trying to unmarshal a root object directly, without it being a field in another object, and therefore bypassing the @XmlJavaTypeAdapter

I'd rather not wrap my objects because this will change the xml that will be serialized in our database. And it sounds like it's possible to call the XMLAdapter directly, going by these answers elsewhere:

[1] http://www.coderanch.com/t/505457/XML/jaxb-xmladapter-rootElement [2] http://markmail.org/message/etvbyzn3e3idpa7q#query:+page:1+mid:cetzvq37nifr6tk6+state:results [3] JaxB inheritance marshalling abstract classes

But I couldn't find out how you would do that :(

I would have guessed it would be something like

AdaptedFoo adaptedFoo = (new MyAdapter()).unmarshall(fooXml)

But that's not the interface to the XMLAdapter unmarshal method. In my case the xml needs to be converted to an AdaptedFoo object before it can be unmarshalled.

eg.  public BackgroundJob unmarshal(AdaptedFoo adaptedFoo)

Do I need an extra unmarshalling step to convert myXml to an AdaptedFoo object and then call the my XMLAdapter to convert it to the intended subclass? Or is there a more elegant way?

What's the recommend process?

Was it helpful?

Solution

For the root object you would need to do something like:

XmlAdapter<AdaptedFoo, Foo> xmlAdapter = new FooAdapter();
AdaptedFoo adaptedFoo = (AdaptedFoo) unmarshaller.unmarshal(xml);
Foo foo = xmlAdapter.unmarshal(adaptedFoo);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top