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

前端 未结 3 1201
有刺的猬
有刺的猬 2020-12-03 06:54

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.

相关标签:
3条回答
  • 2020-12-03 07:39

    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)

    0 讨论(0)
  • 2020-12-03 07:40

    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

    0 讨论(0)
  • 2020-12-03 07:41

    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

    0 讨论(0)
提交回复
热议问题