#Converting Java object to XML.

1 messages · Page 1 of 1 (latest)

wraith comet
#

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.

dusk brookBOT
#

<@&987246399047479336> please have a look, thanks.

dusk brookBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.