package at.gv.egiz.moazs; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.xml.bind.*; import java.io.ByteArrayInputStream; import java.io.StringWriter; @Component public class MyMarshaller { // @Value("classpath:mzs/app2mzs.xsd") // private Resource mzsSchema; // // @Value("classpath:mzs/mzs_mypersondata_en.xsd") // private Resource mzsPersonSchema; // // @Value("classpath:zusemsg/zuse_p2.xsd") // private Resource msgSchema; // // @Value("classpath:zusemsg/zuse_mypersondata_en_p2.xsd") // private Resource msgPersonSchema; @Autowired private JAXBContext context; public byte[] toBytes(JAXBElement element) { try { Marshaller marshaller = context.createMarshaller(); StringWriter sw = new StringWriter(); marshaller.marshal(element, sw); return sw.toString().getBytes(); } catch (JAXBException e) { throw new RuntimeException(e); } } public JAXBElement toJAXBElement(byte[] bytes) { try { Unmarshaller unmarshaller = context.createUnmarshaller(); var stream = new ByteArrayInputStream(bytes); return (JAXBElement) unmarshaller.unmarshal(stream); } catch (JAXBException e) { throw new RuntimeException(e); } } }