How do I write a JavaBean to an XML file using JOX?
Author: Deron Eriksson
Description: This Java tutorial describes how to write a JavaBean to an XML file using JOX (Java Objects in XML).
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1
(Continued from page 1)
When we execute our JoxWriteJavaBeanToFile class and refresh our project, we can see that the jox-test.xml file appears at the root level of our project folder.
The generated jox-test.xml file contains the bean data in a very readable form.
<?xml version="1.0" encoding="ISO-8859-1"?> <JoxTest> <testBean2Array> <testBean2String1>alpha</testBean2String1> <testBean2String2>beta</testBean2String2> </testBean2Array> <testBean2Array> <testBean2String1>gamma</testBean2String1> <testBean2String2>delta</testBean2String2> </testBean2Array> <testBoolean>true</testBoolean> <testString>test bean says hi</testString> </JoxTest>
A great thing about this is that the contents of the bean have been 'serialized' into an xmlW form that is very readable. Of course, given your requirements, this may be a bad thing, but in general this is a very good thing. If you've ever stored serialized JavaSW objects in a file system or a databaseW (using the normal Java serializationW mechanisms), you may have run into complexities trying to determine what was in the serialized objects (ie, you need to deserialize them to read them). The great thing about having the objects stored as XML in this fashion is that you can view and edit your data within your objects in the XML form with no deserialization required. Awesome!