Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default value in JAXB

Tags:

java

xml

jaxb

I have an xml file as following and when the filePath2 is null or empty I want the value of that to be of filePath1's value. Is there a way in which I can achieve this through JAXB.

<file filePath1="C:/filePath">
   <subFile name="Test">
      <filePath2></filePath2>   
   </subFile>
<file/>

I don't want to hardcode the default value. If the value for filePath2 is null or blank("") I want to set the filePath1 attribute as the value of 'String filePath'. Is there a way to do it via a setter in JAXB?

like image 306
Madz Avatar asked Mar 27 '12 08:03

Madz


1 Answers

Using plain Oracle JAXB I only see the possibility to implement that using an javax.xml.bind.Unmarshaller.Listener. Implement that interface in your model class and perform the necessary checks in the afterUnmarshal(..) method.

There you can access the value of filePath1 and set (if necessary) it to filePath2.

like image 200
Robert Avatar answered Sep 18 '22 22:09

Robert