MyXmlValidator.java - Unmarshal XML File with Schema Validation

Q

How to add XML schema validation during the unmarshalling process with JAXB API?

✍: FYIcenter.com

A

If you want to perform unmarshal-time XML Schema validation on the input XML file, you need to do the following:

1. Create a SchemaFactory instance and a Schema instance with the XML schema file, User.xsd:

      SchemaFactory f = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
      Schema s = f.newSchema(new File("User.xsd"));

2. Create an Unmarshaller instance and assign the Schema instance:

      JAXBContext c = JAXBContext.newInstance("com.fyicenter.demo");    
      Unmarshaller m = c.createUnmarshaller();
    m.setSchema(s);

3. Call the m.unmarshal() method to unmarshal the input XML file. It will perform the XML Schema validation and return more detailed errors for invalid elements or attributes.

Here is the entire example program,

// Copyright (c) FYIcenter.com
import com.fyicenter.demo.User;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import java.io.*;

public class MyXmlValidator {
   public static void main(String[] args) throws Exception {
      String x = "User.xml";
      if (args.length < 1) {
          System.out.println("USAGE: java MyXmlValidator xml");
          System.exit(-1);
      }
      x = args[0];

    System.out.println("Unmarshal XML file to user object...");
      JAXBContext c = JAXBContext.newInstance("com.fyicenter.demo");    
      Unmarshaller m = c.createUnmarshaller();

      SchemaFactory f = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
      Schema s = f.newSchema(new File("User.xsd"));
    m.setSchema(s);
      User u = (User) m.unmarshal(new FileInputStream(x));

    System.out.println("My user object:");
    System.out.println("   Name: "+u.getName());
    System.out.println("   BirthDate: "+u.getBirthDate());
    System.out.println("   Sex: "+u.getSex());
    System.out.println("   ID: "+u.getID());
    }
}

Compile and run MyXmlValidator.java as shown below.

fyicenter> java -cp .:../jaxb-ri/mod/jaxb-api.jar:\
  ../jaxb-ri/mod/jaxb-runtime.jar:\
  ../jaxb-ri/mod/istack-commons-runtime.jar:\
  ../jaxb-ri/mod/javax.activation-api.jar \
  MyXmlValidator.java UserError2.xml
Unmarshal XML file to user object...
Exception in thread "main" javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 16; 
  cvc-datatype-valid.1.2.1: 'two' is not a valid value for 'integer'.]
  at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:340)
  at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:578)
  at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:264)
  ...

fyicenter> \local\jdk-1.8.0\bin\javac MyXmlValidator.java
fyicenter> \local\jdk-1.8.0\bin\java MyXmlValidator UserError2.xml
Unmarshal XML file to user object...
Exception in thread "main" javax.xml.bind.UnmarshalException - with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 16; 
   cvc-datatype-valid.1.2.1: 'two' is not a valid value for 'integer'.]
   at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335)
   at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:563)
   at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:249)
   ...

 

FAQ for jaxb-*.jar - Java Architecture for XML Binding

Unmarshaller with No Default Data Validation

Examples for JAXB (Java Architecture for XML Binding)

⇑⇑ FAQ for jaxb-*.jar - Java Architecture for XML Binding

2017-06-30, 1808🔥, 0💬