package at.gv.egiz.moazs; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.oxm.jaxb.Jaxb2Marshaller; import org.springframework.stereotype.Component; 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 */ @Component public class Marshaller { @Autowired private Jaxb2Marshaller 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)); } }