简体   繁体   English

如何从同一对象模型封送/解组不同的xml

[英]How to marshal / unmarshal different xml from the same object model

I'm new to JAXB and would appreciate a little help. 我是JAXB的新手,希望能有所帮助。

First let me give you a bit of back-ground. 首先,让我给您一些背景知识。

  1. I have a relatively simple object model, based on 2 main classes: Provider and Product. 我有一个基于2个主要类的相对简单的对象模型:提供者和产品。 Now Provider references a list of Product he can provide, and Product references a list of Provider who can provide it. 现在,提供者引用了他可以提供的产品列表,而产品引用了可以提供该产品的提供者列表。 That is, I have circular references! 也就是说,我有循环引用!

  2. Product is build as a composite pattern, because: a) I do not want to be limited by "categories", "types", ..., but want to create a as flexible Product tree as possible. 产品被构建为复合模式,因为:a)我不想受到“类别”,“类型”,...的限制,而是希望创建尽可能灵活的产品树。 b) I want to manage Product and tree element (the so called "categories") through the same interface. b)我想通过同一界面管理产品和树元素(所谓的“类别”)。

  3. As we all now, the same abstract Product can have many different "physical implementations" (for example: a car is an abstract Product, whereas a Toyota Yaris is a physical product). 众所周知,同一个抽象产品可以具有许多不同的“物理实现”(例如:汽车是抽象产品,而丰田Yaris是物理产品)。 Therefore, to separate abstract and physical implementations of Products, I implemented a bridge pattern. 因此,为了分离产品的抽象和物理实现,我实现了桥接模式。 Abstract Products are now ProductTemplate (the composite mentioned above) and the physical Product are ProductImplementation. 现在,抽象产品为ProductTemplate(上述组合),而物理产品为ProductImplementation。

I managed to model this using Hibernate (and Spring). 我设法使用Hibernate(和Spring)对此建模。

Now I would like to produce XML strings to let the back and front ends dialogue. 现在,我想生成XML字符串以使后端和前端对话。

At the moment, I'm doing some research with JAXB to see if it could match my needs: 目前,我正在对JAXB进行一些研究,看它是否可以满足我的需求:

  1. I would like to marshal a Provider, with the list of all its ProductTemplate 我想封送一个Provider,及其所有ProductTemplate的列表
  2. I would like to marshal a ProductTemplate, with the list of all its Provider 我想封送一个ProductTemplate及其所有提供者的列表

However, this is only one aspect of what I need. 但是,这只是我需要的一方面。 I would also like to be able to produce different versions of XML strings from the same class. 我还希望能够从同一类中生成不同版本的XML字符串。 For example, from a Provider object, I would like to be able to marshal a XML containing 例如,从Provider对象中,我希望能够封送包含以下内容的XML:

  • only its basic infos (Name, Address, ...) 仅其基本信息(名称,地址等)
  • extended infos (last deivered products, last delivered date) 扩展信息(最后衍生的产品,最后交货日期)
  • the full monty, that is, everything I have about him in my BD. 全部财产,即我在BD中关于他的一切。

    At the present, I was able to marshal ProductTemplate, using JAXB anotations, beacause I have anotated them as XmlTransient in Provider class. 目前,我已经能够使用JAXB注释来封送ProductTemplate,因为我已经在Provider类中将它们注释为XmlTransient。 This is way not good enough! 这还不够好!

I'm not asking at this point for detailed instructions with code. 我现在不要求提供有关代码的详细说明。 What I'm asking is : 我要问的是:

It's been a while since I posted this question, and I found the answer to it in Blaise Doughan's blog and MOXy web site. 自从我发布此问题已有一段时间以来,我在Blaise Doughan的博客和MOXy网站中找到了答案。

I don't have much time to detail, but the trick is to use binding files. 我没有太多时间来详细说明,但是诀窍是使用绑定文件。 You may have as many binding files as you want related to a single class. 您可能需要与一个单独的类相关的任意数量的绑定文件。 You just need to link it to your JAXBContext. 您只需要将其链接到您的JAXBContext。

I did not find it easy to use, but it comes handy when marshalling classes with cyclick references. 我发现它不容易使用,但是在用Cyclokk引用编组类时很方便。

Hope it helps and hope I'll have time to explain in more details some day :) 希望能有所帮助,并希望有一天我有时间详细解释:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM