JaxbUtil { @SuppressWarnings("unchecked") public static <T> T convertTo(byte[] xml, JAXBContext context, Schema validationSchema, ValidationEventHandler validationEventHandler) { try (ByteArrayInputStream bis = new ByteArrayInputStream(xml);) { Unmarshaller unmarshaller = context.createUnmarshaller(); if (validationSchema != null) unmarshaller.setSchema(validationSchema); if (validationEventHandler != null) unmarshaller.setEventHandler(validationEventHandler); return (T) unmarshaller.unmarshal(bis); } catch (JAXBException | IOException e) { LOG.error("Failed to unmarshall ", e); return null; } } private  JaxbUtil(); static JAXBContext createContextIfNull(Class<T> clazz, JAXBContext context); @SuppressWarnings("unchecked") static T convertTo(byte[] xml, JAXBContext context, Schema validationSchema, ValidationEventHandler validationEventHandler); static String convertToString(T xmlObject, JAXBContext context, Schema validationSchema, ValidationEventHandler validationEventHandler); static Schema createSchemaIfNull(String xsd, Schema schema); static String getErrorLocation(ValidationEvent event); static String getXmlMixedContentAsString(List<Object> contents); static String convertNodeToString(Node node); static Node convertStringToNode(String xmlStr);  }
@Test public void convertTo() throws IOException, URISyntaxException { JAXBContext context = JaxbUtil.createContextIfNull(Resources.class, null); Schema schema = JaxbUtil.createSchemaIfNull(XSD_SCHEMA, null); byte[] xml = Files.readAllBytes(Paths.get(new ClassPathResource(STRINGS_XML).getURI())); Resources resources = JaxbUtil.convertTo(xml, context, schema, null); assertNotNull(resources); }