XMLEncoder 和 PersistenceDelegate

发布于 2024-09-01 16:52:24 字数 378 浏览 6 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-09-08 16:52:24

深入研究我们的代码库,我发现了一个 XMLEncodeFactory 类,它允许为某些类注册 PersistenceDelegates 并处理使用这些委托创建 XMLEncoder 实例。放。代码本身非常简单,但我认为 Java 已经提供了这种功能。

有人告诉我这是在与 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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文