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 marshaller; public Marshaller(Jaxb2Marshaller marshaller) { this.marshaller = marshaller; } public String marshallXml(final T obj) { StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); marshaller.marshal(obj, result); return sw.toString(); } public T unmarshallXml(final InputStream xml) { return (T) marshaller.unmarshal(new StreamSource(xml)); } }