简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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