By itself XMLEncoder and XMLDecoder cannot be used to do what you require: mapping the XML difference between two objects and using this to transform one object into another.
But, if the 2 objects are of the same type, you can work some magic to make it work as you would like to.
What I would do:
A) Creates an object that holds the propertyName/propertyValues pairs. This class will be used to hold the differences between the objects. Even a simple Map(String, Object) is enough. I call this ABDifference for example.
B) Makes up an utility method which by introspection analyses object A and object B and write a ABDifference class holding the property names and values that differs in A and B, holding B values. Use Introspector.getBeanInfo to get the propertyDescriptor and use those to invoke the getter and check the property values using equals.
Example:
OBJECT A (name: "pippo", description: "version A of pippo", age: 25)
OBJECT B (name: "pippo", description: "version B of pippo", age: 27)
ABDIFFERENCE (Map: description--->"version B of pippo", age--->27)
C) Encodes this object of this class, which holds the 'differences' between the two original objects using XMLEncoder. This way you will get a simple XML encoding that just holds the property values that differs from A to B for example, and not the whole object.
D) Do an utility method that transforms A into B by checking ABDifference. It will be implemented easily, by looping on the Key/Value pairs of the map and using reflection to invoke the setter of that property (use Introspector.getBeanInfo to easily get the java beans properties) to transform A into B.
This way you have 2 utility methods that maps the difference between 2 objects of the same class in a special class which holds just the differences. You XMLEncode and transfer this and XMLDecode the other way, then use the object to transform A into B.
ADDITIONAL IMPROVEMENTS: you could even add special treatmeants for common types, like pattern matching for string properties to avoid writing the WHOLE string in the difference, but just the real difference between the 2 strings.
One easy simple way to do this would be find the longest matching "start" and "end" of the string and just encode the difference between them in a special way.
For example you could use this format: $charnumber$, so the encoded string would be $n$central part of the string$m$, where n is the char where the central part to substitute starts and m is the char where the central part to substitute ends. Of course you should escape then the special char chosen ($ in this case) with a double special char and transform it back after.
OBJECT A (name: "pippo", description: "version A of pippo", age: 25)
OBJECT B (name: "pippo", description: "version B of my pippo", age: 27)
ABDIFFERENCE (Map: description--->"$8$B of my$14$", age--->27)
So reading the string difference you would know you have to compose the string like this:
"version " (1-8 chars from A value, like the $8$ tells us)
+
"B of my" (the difference in the diff string between the $8$ $14$
+
"pippo" (chars 14+ from A value)