How to validate XML against XSD in Java With Examples

Java XML Validation API can be used to validate XML against XSD in java program. javax.xml.validation.Validator class is used in this program to validate xml against xsd in java.

Validate XML against XSD

Here are the sample XSD and XML files used.


<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns=""
xmlns:empns="" elementFormDefault="qualified">
	<element name="empRequest" type="empns:empRequest"></element>
	<element name="empResponse" type="empns:empResponse"></element>
	<complexType name="empRequest">
			<element name="id" type="int"></element>
	<complexType name="empResponse">
			<element name="id" type="int"></element>
			<element name="role" type="string"></element>
			<element name="fullName" type="string"></element>

Notice that above XSD contains two root element and namespace also, I have created two sample XML file from XSD using Eclipse.


<?xml version="1.0" encoding="UTF-8"?>
<empns:empRequest xmlns:empns="" xmlns:xsi="" xsi:schemaLocation=" Employee.xsd ">


<?xml version="1.0" encoding="UTF-8"?>
<empns:empResponse xmlns:empns="" xmlns:xsi="" xsi:schemaLocation=" Employee.xsd ">
  <empns:fullName>Pankaj Kumar</empns:fullName>

Here is another XML file that doesn’t confirms to the Employee.xsd.


<?xml version="1.0"?>
	<role>Java Developer</role>

Here is the program that is used to validate all three XML files against the XSD. The validateXMLSchema method takes XSD and XML file as argument and return true if validation is successful or else returns false.

package com.journaldev.xml;
import javax.xml.XMLConstants;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;
public class XMLValidation {
    public static void main(String[] args) {
      System.out.println("EmployeeRequest.xml validates against Employee.xsd? "+validateXMLSchema("Employee.xsd", "EmployeeRequest.xml"));
      System.out.println("EmployeeResponse.xml validates against Employee.xsd? "+validateXMLSchema("Employee.xsd", "EmployeeResponse.xml"));
      System.out.println("employee.xml validates against Employee.xsd? "+validateXMLSchema("Employee.xsd", "employee.xml"));
    public static boolean validateXMLSchema(String xsdPath, String xmlPath){
        try {
            SchemaFactory factory =
            Schema schema = factory.newSchema(new File(xsdPath));
            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(new File(xmlPath)));
        } catch (IOException | SAXException e) {
            System.out.println("Exception: "+e.getMessage());
            return false;
        return true;

Output of the above program is:

EmployeeRequest.xml validates against Employee.xsd? true
EmployeeResponse.xml validates against Employee.xsd? true
Exception: cvc-elt.1: Cannot find the declaration of element 'Employee'.
employee.xml validates against Employee.xsd? false

The benefit of using Java XML validation API is that we don’t need to parse the file and there is no third party APIs used.

By admin

Leave a Reply

%d bloggers like this: