I would like to add a processing instruction whenever a collection/array property is serialized to get something like
<alice>
<? array bob ?>
<bob>edgar</bob>
<bob>david</bob>
</alice>
Is this possible with JAXB? Or at least with some specific JAXB implementation?
You could leverage an
XMLStreamWriter
and anXmlAdapter
to do this:BobAdapter
Things to note about the
XmlAdapter
:XMLStreamWriter
. We will later leverage JAXB's ability to set a statefulXmlAdapter
on aMarshaller
.List<String>
to aList<String>
, we're just using anXmlAdapter
here to get an event.marshal
method is where we callwriteProcessingInstruction
on theXMLStreamWriter
:Alice
The
@XmlJavaTypeAdapter
annotation is used to link theXmlAdapter
with the field/property:Demo
input.xml
Output
Note
This example needs to be run with EclipseLink JAXB (MOXy) as the JAXB RI will throw the following exception (I'm the MOXy lead):
For More Information
UPDATE
I have entered an enhancement request (https://bugs.eclipse.org/354286) to add native support for processing instructions. This would eventually allow you to specify the following on your property: