In this tutorial, we will use XML Validation API to validate XML vs XSD in Java. Specifically, we will use the javax.xml.validation.Validator class to validate XML vs XSD in this program.
Now, we see the sample XSD and XML files, below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.javabycode.com/Fruit" xmlns:fruitns="https://www.javabycode.com/Fruit" elementFormDefault="qualified"> <element name="fruit" type="fruitns:fruit"></element> <complexType name="fruit"> <sequence> <element name="id" type="int"></element> <element name="name" type="string"></element> <element name="produceby" type="string"></element> </sequence> </complexType> </schema> |
And you should create xml file from xsd file by using eclipse. See the figure below.

You will be get the xml such as:
Fruit.xml
1 2 3 4 5 6 |
<?xml version="1.0" encoding="UTF-8"?> <fruitns:fruit xmlns:fruitns="https://www.javabycode.com/Fruit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.javabycode.com/Fruit Fruit.xsd "> <fruitns:id>1</fruitns:id> <fruitns:name>Apple</fruitns:name> <fruitns:produceby>Chile</fruitns:produceby> </fruitns:fruit> |
This tutorial shows true validation case and false validation case, so you should create other xml file such as:
OtherFruit.xml
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="UTF-8"?> <otherfruit> <id>1</id> <name>Apple</name> <produceby>Chile</produceby> <comment>Chile</comment> </otherfruit> |
The program that is implemented to validate two files XML against the XSD. The main point of this program is the validateXML method that returns true if validation is successful or else returns false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.javabycode.xml; import java.io.File; import java.io.IOException; import javax.xml.XMLConstants; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import org.xml.sax.SAXException; public class ValidateXMLSample { public static void main(String[] args) { System.out.println("Fruit.xml validates versus Fruit.xsd? " + validateXMLSchema("src/main/resources/Fruit.xsd", "src/main/resources/Fruit.xml")); System.out.println("OtherFruit.xml validates versus Fruit.xsd? " + validateXMLSchema("src/main/resources/Fruit.xsd", "src/main/resources/OtherFruit.xml")); } public static boolean validateXMLSchema(String xsdPath, String xmlPath) { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 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; } } |
Here is the output:
1 2 3 |
Fruit.xml validates versus Fruit.xsd? true Exception: cvc-elt.1: Cannot find the declaration of element 'otherfruit'. OtherFruit.xml validates versus Fruit.xsd? false |
Hope that this tutorial is interesting with you. All suggestions are welcome.
Happy learning!
You can download source of the program
ValidateXML.zip