« Annotation Inheritance | Main | Independent mapping of methods and parameters »

May 26, 2006

Mapping complex objects to XML: Cyclic reference problems

I'm trying to map complex objects to XML to be able to transfer the object data from a client to a Web service. The data fields are transferred only, i.e. the object is mapped and not serialized (like Java serialization).

Mapping simple data type fields is easy. Mapping references to other objects is done by including the referenced object as child XML nodes.

A simple example (simplified version, better would be getter/setter methods):


public class Person{
  public String name;
  public Person relatedTo;
}

A person with a reference to another person could be mapped like this:


 <person>
  <name>Donald</name>
  <relatedTo>
   <person>
    <name>Daisy</name>
    <relatedTo />
   </person>
  </relatedTo>
 </person>

So Donald is related to Daisy, but Daisy not related to Donald. If the inclusion process is continued, it would cause problems if Daisy is related to Donald, too. These are cyclic references and this way of mapping would include Donald in Daisy and Daisy in Donald continuing unlimitedly. There needs to be another way of mapping cyclic references.

A possible solution:

The same mapping with Donald related to Daisy and the other way round looks like this:


 <person id="1">
  <name>Donald</name>
  <relatedTo>
   <person id="2">
    <name>Daisy</name>
    <relatedTo ref="1" />
   </person>
  </relatedTo>
 </person>

Posted by Dominik Marks at May 26, 2006 2:42 PM