How to validate an XML against schema using JAXB?

32,835

You just need to set an instance of javax.xml.validation.Schema on the Unmarshaller before you do the unmarshal. You can specify an implementation of ValidationEventHandler on the Unmarshaller to catch any problems that occur during the unmarshal process.

For More Information

I have written more about this use case on my blog:

Share:
32,835
AKIWEB
Author by

AKIWEB

Updated on July 09, 2022

Comments

  • AKIWEB
    AKIWEB almost 2 years

    I am working with XML and JAXB as I am unmarshalling and marshalling the XML into Java objects and vice versa. Now I am trying to validate our XML against our schema(test.xsd). Suppose if any required field is missing in my XML, then I would like to know which field is missing after validating the XML against schema test.xsd.

    public void unmarshal(final InputStream is) {
        final XMLInputFactory factory = XMLInputFactory.newInstance();
        final XMLStreamReader reader = factory.createXMLStreamReader(is);
    
        Object req = unmarshaller.unmarshal(reader);
    
        // how would I validate here?
    }
    

    How would I validate my XML against test.xsd schema. My test.xsd schema path is -

    C:\workspace\one\two\three\src\main\java\com\package\serv\ap\versionOne\test.xsd

    UPDATE: loading test.xsd as:

    Schema schema = factorySchema.newSchema(new File("C:\\workspace\\one\\two\\three\\src\\main\\java\\com\\package\\serv\\ap\\versionOne\\test.xsd"));
    
  • AKIWEB
    AKIWEB almost 10 years
    Thank you Blaise, that was indeed a useful information. I have one quick question, while loading the test.xsd the path I am giving is absolute right now, I am not sure how do I set the relative path in order to have it loaded. Anything on this would be great.Updated my question regardign the same
  • bdoughan
    bdoughan almost 10 years
    @akiiddweeber - You should be able to use a relative path. You just need to ensure the path is relative to what your Java VM considers the working directory.
  • AKIWEB
    AKIWEB almost 10 years
    Thanks and yes that's correct, so as updated in my question, if that is my absolute path, how should I find out the relative path in intelliJ :( Generally it do pick from resource but I am clueless why is it not picking it up from there (as relative) :(
  • bdoughan
    bdoughan almost 10 years
    @akiiddweeber - You could pick it up as a resource (from the classpath) as well using a ClassLoader. Generally when I need to pick something up from a relative path and I don't know the working directory I specify my best guess, and then based on the error which usually contains a full path I figure out what my relative path should be. IDE's also usually provide a config setting to specify what you want the working directory to be.
  • AKIWEB
    AKIWEB almost 10 years
    Thank you @ Blaise it kind of worked, I just did the following: Schema schema = factorySchema.newSchema(this.getClass().getResource("//test.‌​xsd")); and changed the single-slash operator to double- Strange but sort of worked, still testing but looks like its fixed :)