使用JAXB将XML解组到现有对象

时间:2021-01-18 21:50:38

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

我有一个xsd生成一组现有的java类,目前它成功解组XML消息到对象如预期的那样,但是我希望能够做的是,我有一个现有的实例对象的解组程序简单地更新字段包含在消息传递给它

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

如果我有一个带注释的类图书,其中包含许多字段、标题、作者、已发布等等,以及相应的生成的xsd,那么如果我收到以下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.

与其简单地创建一个只有标题集的新Book实例,不如将其应用到一个现有实例作为更新,因此只需在该实例上设置title变量。

3 个解决方案

#1


5  

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.

JAXB不能帮你,不。但是,您可以使用JAXB将XML文档分解到一个新对象上,然后以反射的方式将新对象的属性复制到现有对象上。

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

普通的BeanUtils提供了一种机制,例如BeanUtils。copyProperties方法。不过,我不确定这是否有深度拷贝。

#2


0  

You make this work with JAXB, but you have to do most of the work yourself.

您可以使用JAXB完成这项工作,但大部分工作都需要您自己完成。

In your example you need a look up capability, like a singleton registry of titles to book objects.

在您的示例中,您需要查找功能,比如图书对象的单例标题注册。

Since your identifier, title, is a child element, there is no guarantee that JAXB will visit the title element before any other element. The easiest way around this is to just copy properties from the Book instance created by the Unmarshaller to your singleton Book instance. The afterUnmarsal callback is the place to do this:

由于您的标识符(title)是一个子元素,所以不能保证JAXB在任何其他元素之前访问title元素。最简单的方法是从Unmarshaller创建的Book实例复制属性到单例书实例。afterUnmarsal回调是这样做的地方:

class Book {
 // ...
 private void afterUnmarshal(Unmarshaller reader, Object parent) { 
   Book singleton = BookRegistry.getInstance().getBook(this.title);
   if (this.publisher != null) singleton.setPublisher(this.publisher);
   if (this.author != null) singleton.setPublisher(this.publisher);
   // ...
 }
}

If you want to register objects out of the XML document on the fly, it is a little more complex. In your case, you probably want something like:

如果您想动态地从XML文档中注册对象,那么这就有点复杂了。在你的情况下,你可能想要:

class Book {
 // ...
 public void setTitle(String title) {
    this.title = title;
    Book singleton = BookRegistry.getInstance().getBook(this.title);
    if (singleton == null) BookRegistry.getInstance().addBook(this.title, this);
 }
}

which will work as long as you don't have nested Books.

只要你没有嵌套的书就可以。

#3


0  

You can achieve this using Copyable plug-in from JAXB2 Commons - Copyable+plugin

您可以使用来自JAXB2 Commons - Copyable+插件的可复制插件来实现这一点

#1


5  

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.

JAXB不能帮你,不。但是,您可以使用JAXB将XML文档分解到一个新对象上,然后以反射的方式将新对象的属性复制到现有对象上。

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

普通的BeanUtils提供了一种机制,例如BeanUtils。copyProperties方法。不过,我不确定这是否有深度拷贝。

#2


0  

You make this work with JAXB, but you have to do most of the work yourself.

您可以使用JAXB完成这项工作,但大部分工作都需要您自己完成。

In your example you need a look up capability, like a singleton registry of titles to book objects.

在您的示例中,您需要查找功能,比如图书对象的单例标题注册。

Since your identifier, title, is a child element, there is no guarantee that JAXB will visit the title element before any other element. The easiest way around this is to just copy properties from the Book instance created by the Unmarshaller to your singleton Book instance. The afterUnmarsal callback is the place to do this:

由于您的标识符(title)是一个子元素,所以不能保证JAXB在任何其他元素之前访问title元素。最简单的方法是从Unmarshaller创建的Book实例复制属性到单例书实例。afterUnmarsal回调是这样做的地方:

class Book {
 // ...
 private void afterUnmarshal(Unmarshaller reader, Object parent) { 
   Book singleton = BookRegistry.getInstance().getBook(this.title);
   if (this.publisher != null) singleton.setPublisher(this.publisher);
   if (this.author != null) singleton.setPublisher(this.publisher);
   // ...
 }
}

If you want to register objects out of the XML document on the fly, it is a little more complex. In your case, you probably want something like:

如果您想动态地从XML文档中注册对象,那么这就有点复杂了。在你的情况下,你可能想要:

class Book {
 // ...
 public void setTitle(String title) {
    this.title = title;
    Book singleton = BookRegistry.getInstance().getBook(this.title);
    if (singleton == null) BookRegistry.getInstance().addBook(this.title, this);
 }
}

which will work as long as you don't have nested Books.

只要你没有嵌套的书就可以。

#3


0  

You can achieve this using Copyable plug-in from JAXB2 Commons - Copyable+plugin

您可以使用来自JAXB2 Commons - Copyable+插件的可复制插件来实现这一点