5

Is it possible to marshal XML directly onto an existing object and have only specific fields updated?

I would like to only modify certain fields that are requested by the client.

Any pointers ?

jahroy
  • 22,322
  • 9
  • 59
  • 108
  • It can be done with reflection. Depending on how complex the types are, it could be pretty straightforward. Curious to see if any libraries are recommended. – jahroy Nov 17 '12 at 09:23
  • The objects to be unmarshalled are not in my control. They can house any kind of member types, including other user-defined classes as well. In this case, it is possible to use reflection ? Should I also use Introspection ? Any examples available for the same ? – Prasanna Viswakumar Nov 19 '12 at 18:11
  • That is the point of reflection: you can inspect any Object without knowing anything about it. I won't be able to work on an example today, but will see if I can post some code later tonight (maybe in 12 hrs). – jahroy Nov 19 '12 at 19:00
  • HI, do you have an example for this ? Thanks much in advance. – Prasanna Viswakumar Nov 26 '12 at 15:29
  • maybe this can help you http://dozer.sourceforge.net/documentation/whymap.html – cproinger May 01 '13 at 18:38

2 Answers2

0

The solution is : Annotations and Reflection. and no there are no libraries for the same. I searched a lot when I was doing this task.

I have done exactly the same thing as you want, I have done my own implementation for this. But it is proprietary so I cannot share.

But to hint you on solution: I have used annotations to mark the fields that are Reloadable and then used Reflection to reload only the parts that are reloadable.

Step 1: Again unmarshal the xml to create a new Object

Step 2: Call the setter methods on the old object with the new values from the newly unmarshalled object.

Hope this helps.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
  • But I see from above comments that the source is not in your control, then Reflection can help here, provided that there are public setter methods to the properties you are trying to reload. – Narendra Pathai Nov 29 '12 at 12:19
0

The short answer is no. There is no easy way to customize the jaxb bindings thinking of a generic solution. See JAXB and class instantiation

Another solution (maybe cruder) would be to extract the XML part you want to process and then execute the unmarshall. See Java How to extract a complete XML block

Community
  • 1
  • 1
Andrés Oviedo
  • 1,388
  • 1
  • 13
  • 28