使用JAXB将XML解组为现有对象 [英] Unmarshalling XML to existing object using JAXB

查看:130
本文介绍了使用JAXB将XML解组为现有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从一组现有java类生成的xsd,目前它成功地将XML消息解组到对象中,但是我希望能够做的是我有一个现有的Object实例。 unmarshaller只是简单地更新传递给它的消息中包含的字段

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)

如果我有一个带有许多字段,标题,作者,已发布等的带注释的类书和相应的生成的xsd,很多字段设置为不需要我想能够我收到了以下xml

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>

而不是简单地创建一个只有标题集的新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.

推荐答案

JAXB无法为您做到这一点,不。但是,您可以使用JAXB将XML文档解组为新对象,然后将新对象的属性反射复制到现有对象上。

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 为此提供了一种机制,例如 BeanUtils.copyProperties 方法。不过,我不确定这是否有深拷贝。

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

这篇关于使用JAXB将XML解组为现有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆