I have a fairly complicated data structure that I can't seem to get to unmarshal correctly.
@XmlRootElement
class Tree {
@XmlID
private String id;
@XmlJavaTypeAdapter(type=TreeFooAdapter.class)
Map<Tree, Foo> fooMap;
}
class Foo {
@XmlID
private String id;
}
I have TWO separate trees. Two nodes (one from each tree) can be paired and associated with an instance of Foo. fooMap is used to keep track of what other nodes a given tree node has been paired with, and what instance of Foo results.
The TreeFooAdapter is pretty straightforward, but note that it uses ID refs:
public class TreeFooAdapter extends XmlAdapter<TreeFooAdapter.MapType, Map<Tree, Foo>> {
public static class MapType {
public static class MapEntry {
@XmlAttribute
@XmlIDREF
public Tree key;
@XmlAttribute
@XmlIDREF
public Foo value;
}
// etc...
}
// Standard drill for marshal/unmarshal...
}
THE PROBLEM: Forward references don't work! When unmarshalling, whichever Tree comes first in the XML will have null keys in its fooMap. Since the two trees reference each other, there's no way that I could change the order of the XML to get around this.
I've tried a hack wherein I have a private method to get/set a List<TreeFooMapEntry>
, but it produces the same result.
Why is JAXB unable to handle forward ID references when they're contained in a Map or List, and how can I resolve this?