Created
December 8, 2017 21:24
-
-
Save gabrielstelmach/60f411f620adddf46baf01845439213c to your computer and use it in GitHub Desktop.
Extracting readable XML of JAXBElement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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