如何制作像xmlbean XmlObject.copy()这样的JAXB对象的深层副本? [英] How to make a deep copy of JAXB object like xmlbean XmlObject.copy()?

查看:97
本文介绍了如何制作像xmlbean XmlObject.copy()这样的JAXB对象的深层副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是重构一些使用xmlbeans的组件现在使用jaxb。一切都很顺利,直到我到达前一位作者所谓的其中一个XmlObjects的copy()函数。由于xmlbeans中的所有对象都扩展了XmlObject,因此我们可以免费获得神奇的深度复制函数。

I have been tasked with refactoring some components that used xmlbeans to now make use of jaxb. Everything is going great, until I get to a place where the previous author has called the copy() function of one of the XmlObjects. Since all objects in xmlbeans extend XmlObject, we get the magic deep copy function for free.

Jaxb似乎没有为我们提供此功能。制作Jaxb对象的深层副本的正确和简单方法是什么?

Jaxb does not seem to provide this for us. What is the correct and simple way to make a deep copy of a Jaxb object?

推荐答案

您可以使JAXB类可序列化然后通过序列化和反序列化深度复制对象。代码可能类似于:

You could make your JAXB classes serializable and then deep copying an object by serializing and deserializing it. The code might look something like:

Object obj = ...  // object to copy

ObjectOutputStream out = new ObjectOutputStream(new ByteArrayOutputStream());
out.writeObject(obj);
byte[] bytes = baos.toByteArray();

ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
Object copy = in.readObject();

这篇关于如何制作像xmlbean XmlObject.copy()这样的JAXB对象的深层副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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