简体   繁体   中英

Best approach to object XML Serialization in Java

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.

JAXB is the standard. In the simplest (and most common case) you just annotate your entities with JAXB annotations, and use a Marshaller to marshal the object to XML.

You can use either Sun's reference implementation , or Apache JaxMe .

XStream is a good alternative as far as I know, although I haven't used it.

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:

<pack.Person>
  <mom>
    <pack.Person>
    ...
    </pack.Person>
  </mom>
  <dad>
    <pack.Person>
    ...
    </pack.Person>
  </dad>
</pack.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");

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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