Keep in mind that there are several bugs related to encoding / decoding java beans with the xml encoder and decoder. You can get an idea by searching the bugs.sun.com website. For example, java.beans.Statement is not thread safe. Most of these issues are being resolved in java 2 SDK version 1.5.
If you want to encode an Object that is not strictly a java bean, you can create a custom PersistenceDelegate that creates Statements / Expressions that describe how to instantiate a new instance of the object and also mutate that instance so that it is equal to the object being encoded. The Statements / Expressions are used to write out the xml document. By doing this, you are basically telling the decoder how to create a new instance of an object and mutate that instance so that it is in the same state as it was when you encoded it. See Using XMLDecoder for details on how this works.
Joined: May 13, 2001
Heh...this thing is somewhat complicated for what I need.
What API should I use in Java for simple XML output?
I want something easy like this pseudo code:
XMLDocument xmlDoc = new XMLDocument("myxml.xml"); XMLElement books = new XMLElement("books"); XMLAttribute size = new XMLAttribute("1"); books.addChild("Gone with the Wind"); books.addAttribute(size); xmlDoc.writeFile();