[jdom-interest] java-object to xml

Patrick Dowler Patrick.Dowler at nrc.ca
Fri Nov 17 09:12:05 PST 2000


On Fri, 17 Nov 2000, you wrote:
> Hi guys!
> Is there a generic class in JDOM that converts java-object and their attributes
> (by reflection) to xml-representation ?
> Thanks in advance !
> Peter

Well, reflection isn't magic. You do need classes with some known behaviour.

One type of class are those where pairs of get/set methods allow you to 
deconstruct/reconstruct the state of the object (aka java beans). The implicit
requirement here is that all the state is accessible via get/set and the order
you can get/set does not matter. 

Another type of class which can be easily handled is one where all the
member variables are public (a Jini Entry object, for example). In this case,
the state is directly visible and you can handle each bit separately. 

In both types, it is sometimes tricky to handle the case where two
references in the class refer to the same object. It is often the case that
serializing and deserializing will result in two distinct but identical objects
instead of a common reference. This is true of Jini entry marshalling, for
example. It is specifically NOT true of java object serialization. It may or
may not be true of an XML-type serialization, but it requires special
handling/encoding.

Personally,  I am using JDOM to write/read Jini entry objects. I am able to
write Lists, Maps, Sets (using the Iterator), objects that implement my own
Writable interface (Element toElement() and void fromElement(Element e),
and the various primitive wrappers (toString and Constructor(String) for now).
This is obviously a data-structure centric use, but it fits my needs and is
pretty flexible. For example, a List can be filled with any of the other objects
that I can write (which become child elements of the list element). 

There are several projects around to write/read java beans (BeanML at
IBM alphaworks?) using XML.

In any case, it is important to remember that you have to make some assumption
about the behaviour of the class. For java beans, you are assuming that the
get/set methods actually de/reconstruct the state properly. 

-- 

Patrick Dowler
Canadian Astronomy Data Centre




More information about the jdom-interest mailing list