Java中对象XML序列化的最佳方法 [英] Best approach to object XML Serialization in Java

查看:1098
本文介绍了Java中对象XML序列化的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写服务以在我们的应用程序中实现审计,其中用户可以在任何修改之前和之后查看特定实体的状态,并且还应该能够回滚。我们决定将XML Serialized对象存储在XML_TYPE列的数据库中。

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.

我是序列化的新手,我不知道如何实现相同,任何更改需要对要序列化的对象进行处理,还是需要有任何映射XML。有人可以建议一些好的库,我知道市场上有很多可用的产品,如JAXB,JIBX,JABX,XStream等。哪一个会好,如何使用它。

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.

非常感谢任何帮助。

问候,
Ravi。

Regards, Ravi.

推荐答案

当然,最好的实体是拥有POJO(普通旧Java对象)。没有奇怪的属性,参考或方法。它简化了序列化,使模型对象与框架和奇怪的层保持中立,如持久性,UI,远程访问等。

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:简单

我建议使用XStream库进行序列化。它试图成为将对象序列化和反序列化为XML的最简单方法。

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

你应该这样考虑searialization:

You should think searialization this way:


  • 表示对象是什么类

  • 尝试序列化每个属性

因此,这些是序列化要解决的两个问题。 XStream允许您创建一个序列化程序(XStream类),(OPTIONALLY)指示每个类使用的标记名称和(OPTIONALLY)指示属性的别名。

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

它将写入没有配置:

<pack.Person>
  <mom>
    <pack.Person>
    ...
    </pack.Person>
  </mom>
  <dad>
    <pack.Person>
    ...
    </pack.Person>
  </dad>
</pack.Person>

但如果你告诉它将package.Person映射到它将使用该标签。你可以告诉它把属性妈妈写成妈妈等等。

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");

参考

XStream还允许您指出所需的引用类型:

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


  • 无引用:序列化对象
    每次在对象中找到它

  • 绝对引用:第二次
    找到一个对象保存使用绝对路径

    引用第一个实例
    (/ people / person [4] / teacher)

  • 相对引用:相同,但
    使用此
    点的相对参考(../../ person [4] / teacher)

  • 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)

这篇关于Java中对象XML序列化的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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