Question

Every time I've searched for something on how to serialize to XML, I found the bean serializers. It seems that everything has to be declared as a property, which defeats my purpose. I want to serialize complex hierarchies (until they get to primitives, of course) in XML, start with (say) a Dictionary or List as the parent object.

What options are there for automatic XML serialization?

I found some references to JAXB from 2004, is that still the way to go?

Was it helpful?

Solution

Try the XStream library. It will easily serialize any complex object hierarchy you can throw at it.

OTHER TIPS

I've had a lot of success with JAXB, especially when using the support for it in NetBeans. I know for a fact it supports lists. I haven't tried it with maps, but I suspect it works. If nothing else, just make a simple bean that contains the list and let that be the serialized bean.

You can use the standard java.bean.XMLEncoder / Decoder classes to serialize collections because the mechanism allows for the calling of methods via XML. The output looks something like:

<object class="java.util.ArrayList">
  <void method="add">
    <string>Hello</string>
  </void>
  <void method="add">
    <string>World</string>
  </void>
</object>

Of course this has weaknesses; for example you cannot easily serialize a Collections.emptyXXXX() but there are workarounds. But because everything works according to the publicly-supported API of the classes, the persistent XML form is resilient to schema changes.

I've found it a very useful persistent mechanism for Java objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top