简体   繁体   中英

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

I'm new to JAXB and would appreciate a little help.

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. 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. b) I want to manage Product and tree element (the so called "categories") through the same interface.

  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). 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.

I managed to model this using Hibernate (and Spring).

Now I would like to produce XML strings to let the back and front ends dialogue.

At the moment, I'm doing some research with JAXB to see if it could match my needs:

  1. I would like to marshal a Provider, with the list of all its ProductTemplate
  2. I would like to marshal a ProductTemplate, with the list of all its Provider

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. For example, from a Provider object, I would like to be able to marshal a XML containing

  • 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.

    At the present, I was able to marshal ProductTemplate, using JAXB anotations, beacause I have anotated them as XmlTransient in Provider class. This is way not good enough!

I'm not asking at this point for detailed instructions with code. What I'm asking is :

  • can JAXB meet my need ?
  • if yes, please suggest a way to do it (I have seen that this post answers part of my questions XSD for having 2 root elements (1 at a time) ), especially the multiple XML from single class thing!

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.

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.

I did not find it easy to use, but it comes handy when marshalling classes with cyclick references.

Hope it helps and hope I'll have time to explain in more details some day :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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