Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmarshalling XML to existing object using JAXB

Tags:

java

xml

jaxb

I have an xsd generated from a set of existing java classes and currently it successfully unmarshalls XML messages into the object as expected, however what I'd like the ability to do is where I have an existing instance of the Object have the unmarshaller simply update the fields that are contained within the message passed to it

for example (forgive any syntax errors here its just off the top of my head)

If I had an annotated class Book with numerous fields, title, author, published etc and corresponding generated xsd, alot of the fields set to being not required I'd like to be able if I received the following xml

<Book>
 <title>Dummys guide to JAXB</title>
</Book>

rather than simply create an new Book instance with only the title set apply this to an existing instance as an update, so simply setting the title variable on that instance.

like image 247
robbo Avatar asked Jul 27 '09 15:07

robbo


1 Answers

JAXB can't do that for you, no. However, what you could do is use JAXB to unmarshal your XML document on to a new object, and then reflectively copy the properties of the new object on to your existing one.

Commons BeanUtils provides a mechanism for this, such as the BeanUtils.copyProperties method. I'm not sure if this does deep-copies, though.

like image 73
skaffman Avatar answered Sep 24 '22 00:09

skaffman