a simple version of this is built into java> = 1.4 using the XMLEncoder
and XMLDecoder
.
quick example
use is pretty simple, line by line
XMLEncoder xmlEncoder = new XMLEncoder( outputStream ); xmlEncoder.writeObject( myObject );
will give you something like
<?xml version="1.0" encoding="UTF-8"?> <java> <object class="your.class.Name"> <void property="fieldName"> <boolean>true</boolean> </void> etc. etc. etc. </object> </java>
to read the object you just do
XMLDecoder xmlDecoder = new XMLDecoder( inputStream ); MyClass thing = (MyClass) xmlDecoder.readObject();
here is a random tutorial i found on google:
http://www.avajava.com/tutorials/lessons/how-do-i-write-a-javabean-to-an-xml-file-using-xmlencoder.html
this method is not surprisingly flexible, but it is built-in, configured, and highly predictable. may be a good starting point.
Some additional notes:
here is a document that describes the xml format: http://java.sun.com/products/jfc/tsc/articles/persistence3/
and here is another link I just found that explains how the transition from XMLEncoder
to jaxb (built in jdk> = 1.6) for more flexibility: http://en.newinstance.it/2010/08/05 / javabeans-to-xml-with-no-libraries /
source share