java xml序列化,Java中对象XML序列化的最佳方法

I am writing the service to implement the audit in our application wherein users can view the status of a particular entity before and after any modification and should also be able to roll it back. We have decided to store the XML Serialized object in the databse in XML_TYPE column.

I am new to serialization, I don't know how to achieve the same, any changes needs to be done to the object to be serialized or do we need to have any mapping XML. Can someone please suggest some good libraries, I understand there are lot of those available in the market like JAXB, JIBX, JABX, XStream and etc. Which one would be good and how to use it.

Any help is highly appreciated.

Regards,

Ravi.

解决方案

Of course, the best for entities is having POJO's (Plain Old Java Objects). No strange properties, references or methods. It simplifies serializing and keeps your model objects neutral from frameworks and strange layers like persistence, UI, remote-access and so on.

XStream: simplicity

I'd suggest using XStream library for serializing. It tries to be the simplest way to serialize and deserialize objects to XML.

You should think searialization this way:

indicate what class is the object

try to serialize each property

So, these are the two problems to resolve in serializing. XStream lets you create a serializer (XStream class), (OPTIONALLY) indicate what tag name use for each class and (OPTIONALLY) indicate the aliases for properties.

So if you have something like:

package pack;

Person

+ mom: Person

+ dad: Person

it will write with no configuration:

...

...

But if you tell it to map package.Person to it will use that tag. You can tell it to write property "mom" as "mother" and things like that.

XStream xs = new XStream();

xs.alias("person", Person.class);

xs.aliasAttribute(Person.class, "mom", "mother");

References

XStream also lets you indicate what kind of references you want:

no references: serialize an object

each time it founds it in the object

tree

absolute references: the second time

an object is found it saves a

reference using the absolute path of

the first instance

(/people/person[4]/teacher)

relative references: the same, but

using a relative reference from this

point (../../person[4]/teacher)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值