简体   繁体   English

如何直接调用JAXB XMLAdapter

[英]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 当我试图直接解组根对象而不是另一个对象中的字段时,我违反了jaxb的XMLAdapters的限制,因此绕过了@XmlJavaTypeAdapter

I'd rather not wrap my objects because this will change the xml that will be serialized in our database. 我宁愿不包装我的对象,因为这将改变将在我们的数据库中序列化的xml。 And it sounds like it's possible to call the XMLAdapter directly, going by these answers elsewhere: 听起来有可能直接调用XMLAdapter,在其他地方通过这些答案:

[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 [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 :( 但我不明白你会怎么做:(

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. 但这不是XMLAdapter unmarshal方法的接口。 In my case the xml needs to be converted to an AdaptedFoo object before it can be unmarshalled. 在我的情况下,xml需要转换为AdaptedFoo对象才能被解组。

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? 我是否需要一个额外的解组步骤来将myXml转换为AdaptedFoo对象,然后调用我的XMLAdapter将其转换为预期的子类? 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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