[英]XMLEncoder and PersistenceDelegate
I'm trying to use XMLEncoder
to write an object graph (tree in my case) to a file. 我正在尝试使用
XMLEncoder
将对象图(在我的情况下为树)写入文件。 However, one class contained in it is not actually a Java bean and I don't particularly like making its guts publicly accessible. 但是,其中包含的一个类实际上并不是Java Bean,并且我不特别喜欢将其胆量公开访问。 It's accessed more like a list and has appropriate
add
/ remove
methods. 它的访问更像一个列表,并且具有适当的
add
/ remove
方法。
I've already written a custom PersistenceDelegate
to deal with that. 我已经写了一个自定义的
PersistenceDelegate
来处理这个问题。 However, it seems that I have to add the PersistenceDelegate
to the XMLEncoder
instance manually. 但是,似乎必须手动将
PersistenceDelegate
添加到XMLEncoder
实例。
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? XMLEncoder
是否可以通过任何方式XMLEncoder
拾取它,或者我真的需要在使用编码器编写可能包含上述类的图时添加它吗?
Digging through our codebase I came across a XMLEncodeFactory
class which allows one to register PersistenceDelegates
for certain classes and handles creating XMLEncoder
instances with those delegates pre-set. 在我们的代码库中,我遇到了一个
XMLEncodeFactory
类,该类允许注册某些类的PersistenceDelegates
并使用预先设置的那些委托处理创建XMLEncoder
实例。 The code itself is very straightforward but I would have thought such a capability would be already offered by Java. 代码本身非常简单,但是我认为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. 有人告诉我,这是在与
XMLEncoder
类的原始开发人员交谈后创建的,显然没有更好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.