How can I have JAXB call a method after it has completed unmarshalling an XML file into an object?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 13:58:07

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)

You can simple add the following method to your object definition:

void afterUnmarshal(Unmarshaller u, Object parent) {
  ...
}

It will be called once the current object has been completely deserialized. See also the documentation about unmarshalling callbacks

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

  • public void beforeUnmarshal(Unmarshaller unmarshaller, Object parent)
  • public void afterUnmarshal(Unmarshaller unmarshaller, Object parent)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!