如何直接调用JAXB XMLAdapter [英] How to invoke JAXB XMLAdapter directly

查看:192
本文介绍了如何直接调用JAXB XMLAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图直接解组根对象而不是另一个对象中的字段时,我对jaxb的XMLAdapters的限制违反了规定,因此绕过了@XmlJavaTypeAdapter

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

我宁愿不包装我的对象,因为这将改变将在我们的数据库中序列化的xml。听起来有可能直接调用XMLAdapter,在其他地方通过这些答案:

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继承编组抽象类

但是我不知道你会怎么做:(

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

我会猜到它会像

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

但这不是XMLAdapter的接口你好nmarshal方法。在我的情况下,xml需要在被解组之前转换为AdaptedFoo对象。

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)

我是否需要额外的解组步骤才能将myXml转换为AdaptedFoo对象然后调用我的XMLAdapter将其转换为预期的子类?或者有更优雅的方式吗?

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?

推荐的流程是什么?

推荐答案

对于根对象,您需要执行以下操作:

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

这篇关于如何直接调用JAXB XMLAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆