Say I have two JavaBeans Person
and Address
.
If I create a list of Person objects, I'd like to marshal to something like this:
<persons>
<person>...</person>
</persons>
It's possible to use the technique described here: Using JAXB to unmarshal/marshal a List<String>
By annotating JaxbList with @XmlRootElement(name = "persons")
and @XmlElement(name = "person")
, then it's possible to marshal to the XML above.
But, it'd be nice to be able to reuse the same JaxbList<T>
class to also marshal a list of Address
objects. And in reality, I will have many other types of beans. I can go with something like:
<list>
<item xsi:type="person" xmlns:xsi="http://www.w2.org/2001/XmlSchema-instance"></item>
</list>
But, ideally, it'd be nice to have it replace "list" with the plural version of class name and "item" with the class name.
So, is it possible to programmatically configure the JaxbContext or something during runtime and essentially set the value of the name
inside @XmlRootElement
and @XmlElement
?
Or any other way to get this working without having to write a separate implementation of JaxbList
for every bean type? Maybe XmlJavaTypeAdapter can achieve this sort of thing?
Update @Blaise Doughan's solution accepted below works great. For my use case, I needed to go straight from Java object to XML, here's what worked (note this is not my full implementation, it's sort of just pseudo code for demonstration):
//JAXBContext is thread safe and so create it in constructor or
//setter or wherever:
...
JAXBContext jc = JAXBContext.newInstance(Wrapper.class, clazz);
...
public String marshal(List<T> things, Class clazz) {
//configure JAXB and marshaller
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//Create wrapper based on generic list of objects
Wrapper<T> wrapper = new Wrapper<T>(things);
JAXBElement<Wrapper> wrapperJAXBElement = new JAXBElement<Wrapper>(new QName(clazz.getSimpleName().toLowerCase()+"s"), Wrapper.class, wrapper);
StringWriter result = new StringWriter();
//marshal!
m.marshal(wrapperJAXBElement, result);
return result.toString();
}
JAXBContext
is thread safe you will see better before if you are able to cache it instead of creating a new one for eachmarshal
operation. – PlenarytoLowerCase()
in that code snippet into your favorite implementation ofdecapitalize()
. – Scarify