I'm trying to use an XmlAdapter
to unmarshal XML
into a custom collection type (that intentionally does not implement the Collection
interface due to a conflicting restriction imposed by another use of the class, which unfortunately fails if the class contains any collections). The difficulty is that the XML
nodes that will be placed into the collection are not wrapped in a wrapper element.
Here's what my XML
essentially looks like:
<xml>
<foo></foo>
<bar>1</bar>
<bar>2</bar>
</xml>
I can make JAXB
unmarshall this to the following POJO
:
class Naive {
Foo foo;
List<Bar> bar;
}
However, what I want to do is the following:
class Bars {
// There will be a fixed number of these, known in advance
Bar bar1;
Bar bar2;
}
class BarsAdapter extends XmlAdapter<ArrayList<Bar>, Bars> {
...
}
class Custom {
Foo foo;
@XmlJavaTypeAdapter(BarsAdapter.class) // won't work
Bars bar;
}
As you can see, I wrote an XmlAdapter
that wants to adapt the entire list, not the individual elements, but it never gets invoked for unmarshalling. (It does get invoked for marshalling.)
If my XML
contained a wrapper around the <bar>
elements, then I know how to solve this:
<xml>
<foo></foo>
<wrapper>
<bar>1</bar>
<bar>2</bar>
</wrapper>
</xml>
because then I could annotate the bars
field with @XmlElement(id="wrapper")
and the adapter would get correctly called. However, the XML
schema comes from an RFC
and is entirely unchangeable, so I am stuck as to how to proceed. Any ideas appreciated!