Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke JAXB XMLAdapter directly

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?

like image 762
moncheery Avatar asked Dec 18 '25 22:12

moncheery


1 Answers

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);
like image 170
bdoughan Avatar answered Dec 21 '25 12:12

bdoughan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!