I’m trying to serialize a list of objects I have in a particular format. The XML structure will be:
<doc>
<data>
<item>
... object properties go here ...
</item>
</data>
</doc>
So far I have the whole structure of an element working properly, but this code below will create it as an XML document itself, I need to be able to loop through my array and add them all to the structure described above.
JAXBContext context = JAXBContext.newInstance(Concern.class);
JAXBElement<Concern> jaxbElement = new JAXBElement(new QName("item"), Concern.class, concerns.get(0));
Marshaller m = context.createMarshaller();
StringWriter sw = new StringWriter();
m.marshal(new JAXBElement(new QName("item"), Concern.class, concerns.get(0)), sw);
Thanks for any help.
You need classes representing the “doc” and “data” tags (say Doc and Data). then you create a Doc instance, add a Data instance to it, and lastly add all your Concern instances to the Data instance. then marshal the Doc instance.