Reputation: 23301
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.
Upvotes: 1
Views: 674
Reputation: 149017
You could do the following:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Doc {
@XmlElementWrapper(name="data")
@XmlElement(name="item")
private List<Concern> concerns;
}
For More Information
Upvotes: 2
Reputation: 53694
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.
Upvotes: 2