XMLEncoder 和 PersistenceDelegate
我正在尝试使用 XMLEncoder
将对象图(在我的例子中为树)写入文件。然而,其中包含的一个类实际上并不是 Java bean,而且我不太喜欢公开其内部内容。它的访问方式更像是一个列表,并且具有适当的 add
/remove
方法。
我已经编写了一个自定义的 PersistenceDelegate 来处理这个问题。但是,似乎我必须手动将 PersistenceDelegate
添加到 XMLEncoder
实例。
有没有办法让 XMLEncoder
自行获取它,或者每当我使用编码器编写可能包含所述类的图形时,我真的需要添加它吗?
I'm trying to use XMLEncoder
to write an object graph (tree in my case) to a file. However, one class contained in it is not actually a Java bean and I don't particularly like making its guts publicly accessible. It's accessed more like a list and has appropriate add
/remove
methods.
I've already written a custom PersistenceDelegate
to deal with that. However, it seems that I have to add the PersistenceDelegate
to the XMLEncoder
instance manually.
Is there any way for XMLEncoder
to pick it up on its own or do I really need to add it whenever I use an encoder to write a graph that may contain said class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
深入研究我们的代码库,我发现了一个
XMLEncodeFactory
类,它允许为某些类注册PersistenceDelegates
并处理使用这些委托创建XMLEncoder
实例。放。代码本身非常简单,但我认为 Java 已经提供了这种功能。有人告诉我这是在与
XMLEncoder
类的原始开发人员交谈后创建的,显然没有更好的替代方案。Digging through our codebase I came across a
XMLEncodeFactory
class which allows one to registerPersistenceDelegates
for certain classes and handles creatingXMLEncoder
instances with those delegates pre-set. The code itself is very straightforward but I would have thought such a capability would be already offered by Java.I was told this was created after conversation with the original developers of the
XMLEncoder
class and there apparently isn't a nicer alternative.