Situation
I use the MOXy JAXB implementation to work with a large XML document whose schema has many similar complex types. In particular, there are about two dozen types that act as elements of a list shell with the following structure:
<ITEMS attr1="X" attr2="Y"> <ITEM>...</ITEM> ... <EXTENSION/> <ITEMS>
For each of these items, similar to list-wrapper, the name changes and the list item contains changes. However, attributes (which are all optional) and a single EXTENSION element (also optional) are always present. Here is an example of using two types:
<ROLES visible="false"> <ROLE type="X"/> <ROLE type="Y"/> </ROLES> <PAYMENTS visible="true"> <PAYMENT> <PAYEENAME>Joe</PAYEENAME> </PAYMENT> <EXTENSION> <SOMETHING>Here</SOMETHING> </EXTENSION> </PAYMENTS>
Question
I would like to avoid code duplication, because the only thing that changes between these elements is the name and one or more elements that it contains. What is the best way to do this?
I can only see two possible solutions.
one
Create a specific class using generics to indicate the type of object that will be used in a changing collection. Then using MOXy's external OX mappings to indicate how any individual class usage should be serialized. Something like:
public class GenericContainer<T> { @XmlAttribute protected Boolean visibile; @XmlElement(name = "Extension") protected Extension extension;
While I like this option, it is not possible to override the OX class mappings for each use.
2
Creating a base class without the List property, and then creating one specific class for each custom wrapper element. This solution definitely works, but in the end I will have about two dozen almost identical classes.
Is there one option or is there a better way to handle this that I haven't thought about?
Thanks in advance!
java design xml jaxb
Terence
source share