![]() |
Linking Java Beans and XML With Ease |
|
JOX Frequently Asked QuestionsWhy should I use JOX instead of one of the other Java-XML libraries? If one of the other libraries fits your needs better than JOX, use it. JOX is useful because it is extremely simple and uses existing formats (Beans, XML, DTD). If you just need to read XML into a Java Bean and vice-versa, JOX is a good tool. Do I have to pay to use JOX? No, JOX is released under the Lesser GPL (LGPL) license, granting you permission to use JOX in commercial and non-commercial applications for free. Unlike regular GPL, LGPL does not force you to license your own software under GPL. I don't have anything against GPL, I just don't want to exclude people who may be writing software for some commercial application. Why doesn't JOX just use reflection so it can access all fields instead of using Java Bean properties? I suppose it's a matter of preference. The idea was that if you write a bean that represents data from an XML document, the bean properties are likely going to correspond to XML tags. You might have a ton of fields that don't have anything to do with the XML. I didn't want to wade through those. Using bean properties weeds out a lot of items that JOX would otherwise have to notice. What kinds of data does JOX understand? JOX understands all the Java primitive types (byte, char, short, int, long, float, double and boolean) and the object-wrapper versions of those types (Byte, Character, Short, etc.). It also understands java.util.Date and String types. If a property is a nested object, it tries to match the nested XML to the nested object. JOX understands indexed properties, too, so it essentially supports arrays. Why doesn't JOX support Hashtables? Hashtables and other mapped data structures are simple to write out to XML, but reading them in is another story. JOX uses bean property types to figure out how to interpret XML data. Since a Hashtable is a container of other kinds of data, there's no way for JOX to figure out how to interpret the data. It could probably do everything as strings, but you may not want that anyway. If you require complex data structures, you might consider looking at one of the other libraries. How can I get JOX to write out XML tags the way I want? Give it a DTD to work with. JOX has a built-in DTD parser that it uses when writing out XML. It looks at the ELEMENT definitions in the DTD to see how to format a particular name (using the same matching rules it uses when reading in data) and also uses the ATTLIST definitions to determine whether to write out a value as an attribute or an element. Using the DTD also limits the output to only the tags in the DTD. That way, you can have properties in the bean that don't map to XML and you can still write out XML that matches the DTD. Got any questions or suggestions? Feel free to write me at mark@wutka.com |