How to invoke JAXB XMLAdapter directly

断了今生、忘了曾经 提交于 2019-12-10 09:52:42

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/23090905/how-to-invoke-jaxb-xmladapter-directly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!