views:

298

answers:

2

I am using JAXB to unmarshall an XML file into a Java object -- standard stuff. Once JAXB has completed this, I'd like a method to be called on the newly created object.

Is there a mechanism to do this? I'd prefer the object, not an external entity, do this to keep construction in one place.

Thanks.

+3  A: 

To be able to execute code after unmarshalling took place, you need an Unmarshaller-Listener

However, I'm not sure, if the listener is invoked after the properties are set or before.

NOTE: The listener is available since JAXB-2.0 (JDK-6)

ivan_ivanovich_ivanoff
Ivan, please reread the question. I want some code executed after the constructor and mutators have been called.
Elliot
I have modified my question
ivan_ivanovich_ivanoff
Thank you, Ivan. I had not found a reference to an Unmarshaller.Listener before -- thanks for the answer.
Elliot
A: 

In addition to the Unmarshaller.Listener you can add the following methods to your domain model classes themselves.

  • public void beforeUnmarshal(Object target, Object parent)
  • public void afterUnmarshal(Object target, Object parent)

From: http://java.sun.com/javase/6/docs/api/javax/xml/bind/Unmarshaller.Listener.html

Blaise Doughan