Java HTML / XML How to - Check a java value with an xml schema using JAXB








Question

We would like to know how to check a java value with an xml schema using JAXB.

Answer

import java.io.File;
// ww w  .  j  a  v  a 2 s  .c  o  m
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.util.JAXBSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

public class Main {

  public static void main(String[] args) throws Exception {
    SchemaFactory sf = SchemaFactory
        .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File("customer.xsd"));

    JAXBContext jc = JAXBContext.newInstance(Customer.class);

    Customer customer = new Customer();
    // populate the customer object
    JAXBSource source = new JAXBSource(jc, customer);
    schema.newValidator().validate(source);
  }

}

class Customer{}