JAXB中的自定义序列化 [英] Custom serialization in JAXB

查看:245
本文介绍了JAXB中的自定义序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在JAXB中自定义XML序列化,就像在.NET中使用IXmlSerializable一样? (即使用相当于XmlReader / Writer直接控制对象序列化的能力)。

Is there a way to customize XML serialization in JAXB, in the same way that it's possible using IXmlSerializable in .NET? (i.e. the ability to directly control serialization of an object using the equivalent of an XmlReader/Writer).

我看了一下XmlAdapter和@XmlJavaTypeAdapter,但是他们似乎只是用于将类型转换为可序列化表单的类型,这不是我想要的。

I've taken a look at XmlAdapter and @XmlJavaTypeAdapter, but they just seem to be used to transform types to and from serializable forms, which isn't quite what I want.

更新:特别是,我想自定义反序列化根对象,以编程方式确定如何反序列化内部XML(例如,创建一个具有特定已知类型集的jaxb unmarshaller)。

Update: In particular, I'd like to customize the deserialization of a root object, that determines, programatically, exactly how to deserialize the inner XML (e.g. create a jaxb unmarshaller with a particular set of known types).

更新:我找到了解决问题的方法,但这是一个非常讨厌的黑客,我可能会选择其他海报提出的解决方案之一。

Update: I've found a way to solve the problem, but it's such a nasty hack I'll probably go with one of the solutions suggested by the other posters.

推荐答案

我不确定我完全理解你的目标是什么,但也许你可以在你的根对象的no-args构造函数中以编程方式做你想做的事情,这将被调用来实例化对象当它是你nmarshalled。

I'm not sure I fully understand what your goal is, but maybe you could do what you want to do programatically inside the no-args constructor of your root object, which will be called to instantiate the object when it is unmarshalled.

这篇关于JAXB中的自定义序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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