How To Schema Check Xml Via JAXB

I recommend that before you load an xml document into JABX that you schema check it. This can be done using the code below. I recommend that you take a local copy of the WSDL file, and check against this, Its not safe to check against a WSDL that is hosted a server that you don't have control over, as the WSDL may change.

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.XMLConstants;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
public class Aclass {
    private static Unmarshaller newInstance(String jaxbContext, String schemaLocation) {
        try {
            final JAXBContext context = JAXBContext.newInstance(jaxbContext);
            Unmarshaller result = context.createUnmarshaller();
            final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            try {
                URI uri = Thread.currentThread().getContextClassLoader().getResource(schemaLocation).toURI()
                final Schema s = schemaFactory.newSchema( toStreamSources(uri));
            } catch (Exception e) {
                throw new RuntimeException(e);
            return result;
        catch (JAXBException e) {
            throw new RuntimeException("Exception occured creating JAXB unmarshaller for context=" + jaxbContext, e);
    static Source[] toStreamSources(@NotNull final URI stream) {
        return new Source[]{new StreamSource(stream.toString())};
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License