How to append externally generated XML instead of variable value of POJO using JAXB?
I have specific data structure as follows:
Map<CustomEnum, CustomList>
Until now, I used utility class to generate xml representation of data in this structure. It uses SAX
to generate the xml output.
For further reading, CustomEnum
is java enum
, CustomList
extends java.util.List
, its implementation is obtained through factory methods
. It allows to contain only objects implementing specific interface
. Their implementations are obtained through factory methods
too, mostly based on specific conditions. I have no access to modify any of those classes.
This is why transforming of this structure through jaxb
seems quite complicated to me, but there are many other reasons, why that utility class was writen (there is lot of conditional evaluating, for example if some values are not null, get data from other values, otherwise provide default values, etc.)
Now this data structure needs to be included as part of bigger structure, based on POJOs and transformed into xml using jaxb.
Something like this:
public class CustomPojo {
...
private String data;
...
private Map<CustomEnum, CustomList> items;
}
XML output should be:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rootElement>
<data> value </data>
...
<items>
<!-- generated xml from my utility class -->
</items>
</rootElement>
What I need is, wherever jaxb marshaller finds member variable of pojo of type Map, instead of trying to generate xml from that variable, to insert xml generated by my utility class.
So I came up with idea to implement custom adapter:
public class CustomAdapter extends XmlAdapter<String, Map<CustomEnum, CustomList>>
using it as
...
@XmlJavaTypeAdapter(CustomAdapter.class)
private Map<CustomEnum, CustomList> items;
and in overriden marshal
method let return xml generated by my utility.
but it gives similar output to this:
<items>
<item itemType="1">
 ...
So as I can see, it is escaped string with xml data instead of xml tags tree.
My question is: Is there way to tell jaxb not to generate xml from property value but insert exterally generated xml as part of its own xml instead?
P.S. Sorry for long description, but I wanted to make a picture as much clear as possible. And please note, that I am aware of that this design is not ideal, but I need this to be working. I am planning general refactoring, but it seems to be a long shot.