0

Based on this example Keep child-parent relationship after unmarshalling I'd like to know if it's possible to unmarshall xml file into an existing model (not having the JAXB annotations). My idea is to directly unmarshall into Primefaces Menu Model (https://www.primefaces.org/docs/api/6.0/org/primefaces/model/menu/package-summary.html). Is it possible?

Rapster
  • 484
  • 1
  • 5
  • 23

2 Answers2

3

JAXB is configuration by exception, so if your model matches the xml, annotations are not necessary.

Here a blog post and a stackoverflow answer by Blaise Doughan on the topic.

martidis
  • 2,897
  • 1
  • 11
  • 13
  • Very interesting thanks! But I have some inheritance in my use case. For example, `BaseMenuModel` (e.g menu) has an attribute called `elements` and can be type of `MenuItem` and/or `SubMenu` – Rapster Feb 14 '18 at 09:56
1

It is possible (if your model is straightforward enough to be JAXB-compatible or with MOXy external mappings), but I would really not recommend it.

If you map to existing model, your XML representation becomes dependent on this existing model. And should the existing model change (like, you update the version of the library you use), you won't be able to unmarshal existing XML and will need migration mechanisms.

From my point of view, it is better to write an XML Schema and compile it to schema-derived classes. Then have a conversion routine to transfrom unmarshalled object structure to the target existing model.

lexicore
  • 42,748
  • 17
  • 132
  • 221