Skip to content

Instantly share code, notes, and snippets.

@gabrielstelmach
Created December 8, 2017 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielstelmach/60f411f620adddf46baf01845439213c to your computer and use it in GitHub Desktop.
Save gabrielstelmach/60f411f620adddf46baf01845439213c to your computer and use it in GitHub Desktop.
Extracting readable XML of JAXBElement
/**
* Extract the XML content, human friendly, of the <b>JAXBElement</b> object.
*
* @param object Object JAXBElement
* @return XML content
* @throws Exception Will be thrown always when object is not a valid JAXB instance.
*/
public static String extractXMLFromJAXBObject(Object object) throws Exception
{
try
{
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
ByteArrayOutputStream output = new ByteArrayOutputStream();
marshaller.marshal(object, output);
return output.toString();
}
catch (Exception ex)
{
throw new Exception("An error happened while extracting XML content of " + object + ": " + ex.getMessage(), ex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment