简体   繁体   English

XMLEncoder和PersistenceDelegate

[英]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.

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