Hello everyone. Im wondering if anyone can help me. So I have Java object that I want to convert to XML. Here is my piece of code:
UnifiedServiceRequest unifiedServiceRequest = createMobileIdAuthUnifiedServiceRequest(authenticationMobileId, customerMobileIdInfo);
logger.info("xml: {}",jaxbObjectToXML(unifiedServiceRequest);
private String jaxbObjectToXML(UnifiedServiceRequest unifiedServiceRequest){
String xmlContent="";
try
{
//Create JAXB Context
JAXBContext jaxbContext = JAXBContext.newInstance(UnifiedServiceRequest.class);
//Create Marshaller
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
//Required formatting??
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//Print XML String to Console
StringWriter sw = new StringWriter();
JAXBElement jx=new JAXBElement(new QName("UnifiedServiceRequest"), UnifiedServiceRequest.class, sw);
//Write XML to StringWriter
jaxbMarshaller.marshal(jx, sw);
//Verify XML Content
xmlContent = sw.toString();
return xmlContent;
} catch (JAXBException e) {
e.printStackTrace();
}
}
unifiedServiceRequest is created successfully, I checked that, method works fine, sets object's fields' values correctly. So this object is ok. But when im running the app, in the place, where im logging the xml, i only get xml: . So my question is how to convert object to xml? Thanks for any help.