Saturday, 7 September 2013

How to unmarshal a non-hierarchical relationship with Java's JAXB library?

How to unmarshal a non-hierarchical relationship with Java's JAXB library?

I'm attempting to unmarshal the following XML with Java's JAXB library.
<message name="GetAllRoutesSoapIn">
<part name="parameters" element="GetAllRoutes"/>
</message>
...
<operation name="GetAllRoutes">
<input message="GetAllRoutesSoapIn"/>
</operation>
It's easy enough to unmarshal this file hierarchically, but I'm having
trouble establishing a relationship between an input and a message.
I'd like the unmarshalled Input class to have a field of type Message
instead of type String.
Input Class
@XmlRootElement
public class Input extends AbstractElement {
@XmlAttribute
private String name;
// @XmlAttribute
// private String message;
// Not sure how to implement this. XMLAdapter?
private Message message;
protected Input () {}
...
My initial idea was to create a custom Message XMLAdapter and annotate
both the Message class, and Input's Message property with a
@XmlJavaTypeAdapter. I'd keep a static collection of all instantiated
Messages on the Adaptor class. The Messages instantiated from the proper
xml element "message" would be collected, and searched through when the
adapter attempted to unmarshal a Message from the input's message
attribute. Came close to working but I can't get the adapter to fire for
the input class.
Any ideas?

No comments:

Post a Comment