package at.gv.egiz.moazs.scheme; import org.springframework.oxm.jaxb.Jaxb2Marshaller; import javax.xml.transform.Result; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.InputStream; import java.io.StringWriter; /** * @author xerx593 * Source: https://stackoverflow.com/questions/44676532/how-to-use-spring-to-marshal-and-unmarshal-xml */ public class Marshaller { private final Jaxb2Marshaller jaxbMarshaller; public Marshaller(Jaxb2Marshaller marshaller) { this.jaxbMarshaller = marshaller; } public String marshallXml(final T obj) { StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); jaxbMarshaller.marshal(obj, result); return sw.toString(); } public T unmarshallXml(final InputStream xml) { return (T) jaxbMarshaller.unmarshal(new StreamSource(xml)); } }