I’m stuck on something that should be very basic.
I have an annotated Person POJO (@XMLAttribute and @XMLElement annotations on the fields).
The class is annotated as @XMLRootElement .
Now I have a service which does a query returning a list of these instances. In other words,
I end up with
List<Person>
I simply want to marshal this list into xml, into my servlet’s response output.
If I just loop through the list and marshal each object individually:
for(Person person : people){
jaxbMarshaller.marshal(person, out);
}
it outputs the WHOLE document multiple times (I end up with multiple xml header lines for each record):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<name>Bob</name>
</person>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<name>Gary</name>
</person>
It shouldn’t output duplicate header lines between the records.
On the other hand, if I try to marshal the whole list with one call:
jaxbMarshaller.marshal(people, out);
I get the following exception:
javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is
known to this context.
What is the ‘correct’ way to simply marshal multiple instances of an annotated POJO?
I assume it must be wrapped in another ‘upper’ root element called ‘people’ and then each person node as a child. But how do you do that with JAXB ?
Thanks
yes , you are right. you will have to take parent element as LIST, then try adding person values in list object.