Which Java XML binding framework supports circular/cyclic dependencies?

841 views Asked by At

I've got two classes:

public class A {
  B refToB;
}

public class B {
  A refToA;
}

they don't have unique id fields (which are required for JAX-B XMLID and XMLIDREF).

Object instances:

A a = new A();
B b = new B();
a.refToB = b;
b.refToA = a;

I want to marshall a to XML while storing the circular/cyclic dependency, something like:

<a id="gen-id-0">
  <b>
    <a ref-id="gen-id-0" />
  </b>
</a>

One of the frameworks I've found that supports this is XStream: http://x-stream.github.io/graphs.html

What other frameworks support this feature ?

Does some JAX-B implementations support it ?

2

There are 2 answers

2
bdoughan On BEST ANSWER

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

MOXy has the @XmlInverseReference extension for mapping bidirectional relationships.

A

import javax.xml.bind.annotation;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class A {
  @XmlElement(name="b")
  B refToB;
}

B

import javax.xml.bind.annotation;
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;

@XmlAccessorType(XmlAccessType.FIELD)
public class B {
  @XmlInverseReference(mappedBy="refToB")
  A refToA;
}

XML

The above classed will map to the following XML

<a>
    <b/>
<a>

For More Information

0
mgaert On

A couple of years ago I worked with Betwixt. They claim support, see http://commons.apache.org/betwixt/faq.html#cycles

Alas, setting up a simple test did not work for me so far, output merely is <A id="1"><B/></A>, with the pointer to A in B silently ignored. There must be some mapping option that I failed to set...