Ketan Bhavsar
Ketan Bhavsar

Reputation: 5396

UnMarshalling with Unique value using JaxB

I have xml returned like,

<UniequeTestList>
 <UniequeTest>
  <FirstName>a</FirstName>
  <LastName>b</LastName>
 </UniequeTest>
 <UniequeTest>
  <FirstName>a</FirstName>
  <LastName>b</LastName>
 </UniequeTest>
</UniequeTestList>

I unmarshal it using JaxB in List<UniequeTest>. And obviously that list must have mulitple object which are same. Can I make it unieque at the same time I unmarshall it?

I can't use HashSet or LinkedHashSet to make it unieque at same time. I mean what I want in Unmarshall list is unieque object if value under that object is same.

Upvotes: 1

Views: 330

Answers (1)

Alexandre Dupriez
Alexandre Dupriez

Reputation: 3036

You can eventually use a listener to the JAXB Unmarshaller:

 class JAXBUnmarshallingListener extends Unmarshaller.Listener {

   @Override
   public void beforeUnmarshal(Object target, Object parent) {
   }

   @Override
   public void afterUnmarshal(Object target, Object parent) {
   }
 }

In the callback method, you can implement a simple cache and replace the object newly created by JAXB by your own unique instance (the new JAXB instance will be garbage collected afterwards as no one keeps referencing it. Note this method won't save you the cost of the instantiation and the gc of these objects.).

Upvotes: 3

Related Questions