tags:

views:

2154

answers:

14

What's the simplest-to-use techonlogy available to save an arbitrary Java object graph as an XML file (and to be able to rehydrate the objects later)?

+4  A: 

Apache digester is fairly easy: http://commons.apache.org/digester/
JAXB is newer and comes with annotation goodness: https://jaxb.dev.java.net

Sietse
A: 

java.beans.XMLEncoder perhaps?

Anarchofascist
+1  A: 

XStream is very simple http://xstream.codehaus.org/

Steve Burton
+7  A: 

The easiest way here is to serialize the object graph. Java 1.4 has built in support for serialization as XML.

A solution I have used successfully is XStream (http://xstream.codehaus.org/)- it's a small library that will easily allow you to serialize and deserialize to and from XML.

The downside is you can only very limited define the resulting XML; which might not be neccessary in your case.

Torsten Uhlmann
+1  A: 

The Simple API is, well, simple! It's really good. http://simple.sourceforge.net/

You can also use XStream: http://www.ibm.com/developerworks/library/x-xstream/index.html

A_M
+3  A: 

XStream by the folks at Codehaus has a simple API and even deals with things like duplicate and circular references. It seems to be actively developed and is well documented.

http://xstream.codehaus.org/

A: 

If you are really only interested in serializing your objects to a file and then deserializing them later, then you might check out YAML instead of XML. YAML is much easier to work with than XML and the output files are very human-readable (which may or may not be a requirement). Check out yaml.org for more information. I've used JYAML successfully on a recent project.

John Shields
+1  A: 

If you need to control the structure of the XML, the XStream is a good choice. You can use annotations to define precisely the structure/mapping of the XML and your objects.

pjesi
+2  A: 

Use java.beans.XMLEncoder. Its API is very simple (actually a little too simple; it'd be nice to wire it to a SAX ContentHandler), but it works on many graphs out of the box, and it's easy to create your own persistence delegate for any odd-ball classes you might encounter.

  • The syntax used by XMLDecoder allows you to invoke any method, instance or static, including constructors, so it's extremely flexible.
  • Other encoders name elements and attributes after class and field names, so there's no fixed schema for the result. The XMLEncoder's XML follows a simple DTD and can easily be validated or transformed, even when you've never seen the types it uses.
  • You can assign objects an identifier, and reference them throughout the graph.
  • You can refer to constants defined in classes or interfaces.

And, it's built into Java SE, so you don't need to ship an extra library.

erickson
A: 

JAX-B is part of the standard APIs and really easy to use.

Andrew Harmel-Law
A: 

I'd second (or third) XStream. It reads and writes XML without needing any special binding configuration or placing lots of extraneous syntax in the XML.

Alan Krueger
+2  A: 

If you need control over the XML that gets generated, I recommend taking a look at Betwixt (http://commons.apache.org/betwixt/) - it adds a lot of functionality to Apache's digester (Digester is good for building object graphs from XML, but is not so good for generating them).

If you really don't care about the XML that gets generated (just that it can be deserialized in the future), then the XMLEncoder/Decoder classes built into Java or good - as long as the objects you are serializing follow the JavaBean specification. The biggest area I've run into problems with the XMLEncoder/Decoder solution is if you have a bean that returns an immutable list for one of it's properties - the encoder doesn't handle that situation very well.

Kevin Day
+2  A: 

Although XStream and JAXB can serialize an some object graphs succssfully they can not handle very complex graphs. The most powerful solution for large complex graphs is http://simple.sourceforge.net/ it can handle any graph. Also, its fast and simple to use without any dependencies.

ng
+1  A: 

I put together a list with a lot of xml serialization libraries and its license

Karussell