views:

208

answers:

0

I am using JAXB to unmarshal an XML file into a Java object model. Works great, except for one thing. I've been using parseMethod and printMethod to convert simple strings to a type of my choice, e.g.:

<someElement tag='Bismarck' name='Dagwood' />
<someElement tag='Pierre' name='Herb' />

where the "tag" attribute, instead of being unmarshalled as a string, gets unmarshalled as a custom type e.g. a Tag class in java.

This works fine, but it relies on parseMethod and printMethod being static methods. I would like to use a marshaller/unmarshaller that is a class instance, that I pass into the Unmarshaller or Marshaller. Is there a way to do this? (I hope so :-( otherwise I'm stuck) I have read up on XMLAdapter and it looks OK, only it seems to require that the type conversion has already occurred.

What I want is for my someElement class in Java to have a method:

Tag getTag()

where Tag is an interface, and I want to install a hook object (an instance of class TagManager in this case) so that the hook object gets to do the unmarshaling from String to Tag, and marshaling from Tag back into String. All the other unmarshaling/marshaling steps are fine as is through the default methods.