I need to get an object graph into xml and back to object again. Get into trouble with cycles. The problem is very clear.
I have an object referencing a node in a tree structure. That nodes holds references to both parent and subtasks. If we always started to advance from the root I understand it would be straightforward to use the afterUnmarshall(Unmarshaller, Object parent) callback to set the parent pointer for the node after the object has been unmarshalled. But not so when we need to traverse the tree starting from a node in the middle of the tree..?? Or should this be a piece of cake also? Anyone wrestled a similar problem?
Solution to your problem: Stop trying to send object graphs over web services.
Despite popular opinion web services exchange messages - not objects or object graphs.
Section 5 SOAP Encoding was the last attempt the translate object graphs into XML. Ultimately it failed because of interoperability problems. Problems:
XML is inherently hierarchical - therefore it cannot represent graphs with cycles all that well.
Even if the server implementation environment can represent objects there is no guarantee the you web service's client can. Furthermore data types that exist on your server platform may not even exist on the client platform. Even if you have types that translate to the same XML Schema type, those types may ultimately have differing semantics on each platform.
I need to beat the vendors over the head to stop doing that, to instead make tools, build tools that want to look at and see and recognize that (A) this is a message and (B) it�s in XML because wanting to take your code and seamlessly turning it into objects and back again, we�ve already talked about that. That�s just not going to work. It�s not going to happen.
Thanks for your time and well thought reply. I can see you are clearly right there. We are moving towards use of web services that actually makes sense, sending the object graphs in their entirety would have made the transition just a little easier for the time being but it seems we will have to go all the way already (which might not be such a bad idea anyhow). So, thanks again for pointing out the problem, might have taken some time to figure out it was a dead end we were looking at..