Java HTML / XML How to - Validate XML against Schema for JAXB entity








Question

We would like to know how to validate XML against Schema for JAXB entity.

Answer

import java.io.File;
import java.util.ArrayList;
import java.util.List;
//  w  ww  .ja v a 2  s .  c o m
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.util.JAXBSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class Main {

    public static void main(String[] args) throws Exception {
        Customer customer = new Customer();
        customer.setName("abcabcabcabcabcabcabc");
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());

        JAXBContext jc = JAXBContext.newInstance(Customer.class);
        JAXBSource source = new JAXBSource(jc, customer);

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new File("customer.xsd"));

        Validator validator = schema.newValidator();
        validator.setErrorHandler(new MyErrorHandler());
        validator.validate(source);
    }

}
 
@XmlRootElement
class Customer {
 
    private String name;
 
    private List<PhoneNumber> phoneNumbers;
 
    public Customer() {
        phoneNumbers = new ArrayList<PhoneNumber>();
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    @XmlElement(name="phone-number")
    public List<PhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    }
 
    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }
 
}
class PhoneNumber {
 
}
class MyErrorHandler implements ErrorHandler {
 
    public void warning(SAXParseException exception) throws SAXException {
        exception.printStackTrace();
    }
 
    public void error(SAXParseException exception) throws SAXException {
        exception.printStackTrace();
    }
 
    public void fatalError(SAXParseException exception) throws SAXException {
        exception.printStackTrace();
    }
 
}

The following are the constraints in the following schema (customer.xsd):

  • The customer's name cannot be longer than 5 characters.
  • The customer cannot have more than 2 phone numbers.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
    <xs:element name="customer">
         
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="stringMaxSize5" />
                <xs:element ref="phone-number" maxOccurs="3" />
             </xs:sequence>
        </xs:complexType>
    </xs:element>
 
    <xs:element name="phone-number">
        <xs:complexType />
    </xs:element>
 
    <xs:simpleType name="stringMaxSize5">
        <xs:restriction base="xs:string">
            <xs:maxLength value="5"/>
        </xs:restriction>
    </xs:simpleType>
 
</xs:schema>