Is there any way we can un-marshall for a class without @XmlRootElement annotation? Or are we obligated to enter the annotation?
for example:
public
I use the generic solution as follows:
public static String objectToXmlStringNoRoot(T obj, Class objClass, final String localPart) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(objClass);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// To format XML
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//If we DO NOT have JAXB annotated class
JAXBElement jaxbElement = new JAXBElement<>(new QName("", localPart), objClass, obj);
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(jaxbElement, sw);
return sw.toString();
}